@theholocron/cli 5.0.0-alpha.402 → 5.0.0-alpha.404

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.
package/dist/cli.mjs CHANGED
@@ -11728,6 +11728,99 @@ function resolveIdeasProvider(resolved, env) {
11728
11728
  }
11729
11729
  };
11730
11730
  }
11731
+ // src/commands/ideas-sync/run-ideas-sync/oldest-issue-key/oldest-issue-key.ts
11732
+ /**
11733
+ * The key of the oldest issue among `keys`, which is the one that wins when overlapping runs filed the same idea.
11734
+ * The `Issue` type carries no creation time, so the order is the key's number (its last run of digits: `#12`, `ENG-7`
11735
+ * and `owner/repo#12` give 12, 7 and 12), which rises with creation in one repository or team; a key with no number
11736
+ * sorts last. Two keys with the same number (different repositories) fall back to a plain string compare so every run
11737
+ * picks the same winner from the same set. Returns `undefined` for an empty list.
11738
+ */
11739
+ function oldestIssueKey(keys) {
11740
+ const numberOf = (key) => {
11741
+ const digits = /(\d+)\D*$/.exec(key);
11742
+ return digits ? Number(digits[1]) : Number.POSITIVE_INFINITY;
11743
+ };
11744
+ let oldest;
11745
+ for (const key of keys) {
11746
+ if (oldest === void 0) {
11747
+ oldest = key;
11748
+ continue;
11749
+ }
11750
+ const a = numberOf(key);
11751
+ const b = numberOf(oldest);
11752
+ if (a < b || a === b && key < oldest) oldest = key;
11753
+ }
11754
+ return oldest;
11755
+ }
11756
+ // src/commands/ideas-sync/run-ideas-sync/pick-canonical-issue/pick-canonical-issue.ts
11757
+ /**
11758
+ * The issue that stands for an idea among the issues that carry its marker: the oldest open one (by
11759
+ * {@link oldestIssueKey}), or, when every one is closed, the oldest of all, because an issue closed on purpose still
11760
+ * means the idea was filed. A closed duplicate that a search lists first therefore never replaces the open issue.
11761
+ * Returns `undefined` for an empty list.
11762
+ */
11763
+ function pickCanonicalIssue(hits) {
11764
+ const open = hits.filter((issue) => issue.statusCategory !== "done");
11765
+ const pool = open.length > 0 ? open : hits;
11766
+ const key = oldestIssueKey(pool.map((issue) => issue.key));
11767
+ return pool.find((issue) => issue.key === key);
11768
+ }
11769
+ // src/commands/ideas-sync/run-ideas-sync/converge-created-issue/converge-created-issue.ts
11770
+ /**
11771
+ * Makes the creation of an issue converge when two runs file the same idea at the same time. Both runs searched for
11772
+ * the marker before either created, so both created; each now searches again, and the oldest issue carrying the exact
11773
+ * `marker` (by {@link pickCanonicalIssue}: the oldest open one) is the winner. Both runs see the same issues, so they pick the same winner: the
11774
+ * run whose issue is the oldest keeps it, and the other comments on its own issue and closes it (`transition` to
11775
+ * `done`; the `Issues` capability has no closed-as-duplicate state), then reports the winner's key. Nothing here throws.
11776
+ * When a winner other than `created` is known, its key is always the one returned, so the note points at the canonical
11777
+ * issue even if commenting or closing the duplicate does not go through (then `warning` is set and the detail names
11778
+ * both keys). When the search itself does not go through no winner is known, so `created` is returned with a warning,
11779
+ * because a created issue is never left out of the note.
11780
+ *
11781
+ * @param issues - The tracker; only `search`, `comment` and `transition` are used.
11782
+ * @param marker - The exact hidden marker line of the idea, as written in the issue body.
11783
+ * @param created - The key of the issue this run has just created.
11784
+ */
11785
+ async function convergeCreatedIssue(issues, marker, created) {
11786
+ let candidates;
11787
+ try {
11788
+ candidates = (await issues.search({
11789
+ text: marker,
11790
+ limit: 100
11791
+ })).filter((issue) => issue.body?.includes(marker) && issue.key !== created);
11792
+ } catch (err) {
11793
+ return {
11794
+ key: created,
11795
+ warning: true,
11796
+ detail: `filed as ${created}; could not check for a duplicate from an overlapping run (${describeError(err)}), so look for another issue with the same marker and close one by hand`
11797
+ };
11798
+ }
11799
+ const winner = pickCanonicalIssue([...candidates, {
11800
+ key: created,
11801
+ statusCategory: "open"
11802
+ }]).key;
11803
+ if (winner === created) return {
11804
+ key: created,
11805
+ warning: false,
11806
+ detail: `filed as ${created}`
11807
+ };
11808
+ try {
11809
+ await issues.comment(created, `Duplicate of ${winner}: the same idea was filed by two overlapping runs.`);
11810
+ await issues.transition(created, "done");
11811
+ } catch (err) {
11812
+ return {
11813
+ key: winner,
11814
+ warning: true,
11815
+ detail: `filed as ${winner}, the older issue for the idea, but the duplicate ${created} could not be closed (${describeError(err)}); close ${created} by hand`
11816
+ };
11817
+ }
11818
+ return {
11819
+ key: winner,
11820
+ warning: false,
11821
+ detail: `filed as ${winner} (closed duplicate ${created} from an overlapping run)`
11822
+ };
11823
+ }
11731
11824
  // src/commands/ideas-sync/run-ideas-sync/run-ideas-sync.ts
11732
11825
  /**
11733
11826
  * `holocron ideas sync` — files the idea notes that are ready.
@@ -11741,7 +11834,9 @@ function resolveIdeasProvider(resolved, env) {
11741
11834
  * with a hidden `<!-- holocron:idea <key> -->` line, and an issue that carries it is reused instead of making a second
11742
11835
  * one (found with one `Issues.search({ text })` per note about to be filed, confirmed by the exact marker in the body;
11743
11836
  * GitHub searches its 2000 most recently updated issues, so an older one is still missed, and each search uses rate
11744
- * limit). It never deletes anything, and an idea it cannot file is a
11837
+ * limit). Two runs that overlap can both miss the marker and both create; each then searches once more after its
11838
+ * create (`convergeCreatedIssue()`), the older issue wins and the other run closes its own with a comment, so both
11839
+ * notes end with one key. It never deletes anything, and an idea it cannot file is a
11745
11840
  * failing row (the run goes on). It writes, so it asks first unless `--dry-run` or `--yes` (without a terminal it refuses instead).
11746
11841
  */
11747
11842
  /** The state that files a note when the config names none. */
@@ -11760,12 +11855,6 @@ const row = (id, status, detail) => ({
11760
11855
  const marker = (key) => `<!-- holocron:idea ${key} -->`;
11761
11856
  /** The issue's body: the idea's text, the line that traces the issue to its idea, and the hidden marker. */
11762
11857
  const issueBody = (idea) => `${idea.body.trim()}\n\nFiled from idea: ${idea.title}\n${marker(idea.key)}`;
11763
- /**
11764
- * How many text matches are read back per note; the exact marker is then checked in each body. The search itself is
11765
- * bounded by the provider: GitHub pages up to 2000 recent issues (20 pages of 100) and filters them client-side, so an
11766
- * issue older than that is still missed, and each search spends rate limit. Linear filters on the server.
11767
- */
11768
- const SEARCH_LIMIT = 20;
11769
11858
  async function runIdeasSync(input) {
11770
11859
  /* v8 ignore next -- the console is only used outside tests, which inject `print` */
11771
11860
  const print = input.print ?? ((line) => console.log(line));
@@ -11842,28 +11931,37 @@ async function runIdeasSync(input) {
11842
11931
  }
11843
11932
  let existing;
11844
11933
  try {
11845
- existing = (await issues.search({
11934
+ existing = pickCanonicalIssue((await issues.search({
11846
11935
  text: marker(path),
11847
- limit: SEARCH_LIMIT
11848
- })).find((issue) => issue.body?.includes(marker(path)));
11936
+ limit: 100
11937
+ })).filter((issue) => issue.body?.includes(marker(path))));
11849
11938
  } catch (err) {
11850
11939
  rows.push(row(path, "fail", `could not check for an existing issue, so none was created: ${describeError(err)}`));
11851
11940
  continue;
11852
11941
  }
11853
11942
  let key;
11943
+ let detail = `already filed as ${existing?.key}`;
11944
+ let warning = false;
11854
11945
  if (existing) key = existing.key;
11855
- else try {
11856
- ({key} = await issues.create({
11857
- summary: idea.title,
11858
- body: issueBody(idea)
11859
- }));
11860
- } catch (err) {
11861
- rows.push(row(path, "fail", `could not create the issue: ${describeError(err)}`));
11862
- continue;
11946
+ else {
11947
+ let created;
11948
+ try {
11949
+ ({key: created} = await issues.create({
11950
+ summary: idea.title,
11951
+ body: issueBody(idea)
11952
+ }));
11953
+ } catch (err) {
11954
+ rows.push(row(path, "fail", `could not create the issue: ${describeError(err)}`));
11955
+ continue;
11956
+ }
11957
+ ({key, detail, warning} = await convergeCreatedIssue(issues, marker(path), created));
11863
11958
  }
11864
11959
  try {
11865
11960
  await ideas.markFiled(path, key);
11866
- rows.push(row(path, "ok", `${existing ? "already filed" : "filed"} as ${key}`));
11961
+ rows.push({
11962
+ ...row(path, "ok", detail),
11963
+ problems: warning ? ["a duplicate issue for this idea may be open: close the extra one by hand"] : []
11964
+ });
11867
11965
  } catch (err) {
11868
11966
  rows.push({
11869
11967
  ...row(path, "fail", `issue ${key} was created but \`issue:\` was not written: ${describeError(err)}`),