@defrex/autobuild 1.4.1 → 1.5.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.
Files changed (201) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +440 -95
  3. package/bin/ab-dispatch-kernel.ts +1 -0
  4. package/docs/accounts.md +9 -0
  5. package/docs/agent-runtimes.md +1 -0
  6. package/docs/answering-blocked-builds.md +2 -1
  7. package/docs/architecture.md +290 -130
  8. package/docs/assets/headline-wide.png +0 -0
  9. package/docs/build-store.md +33 -3
  10. package/docs/cli.md +2 -1
  11. package/docs/configuration-overview.md +9 -0
  12. package/docs/configuration.md +166 -101
  13. package/docs/dispatcher-hosting.md +19 -3
  14. package/docs/dispatcher.md +9 -4
  15. package/docs/integration-overview.md +2 -2
  16. package/docs/integration-tree.md +91 -28
  17. package/docs/merge-policy.md +28 -9
  18. package/docs/operator-api.md +43 -18
  19. package/docs/operator-surfaces.md +1 -1
  20. package/docs/plugin-authoring.md +85 -8
  21. package/docs/remote-store-protocol.md +25 -7
  22. package/docs/remote-ticket-protocol.md +126 -12
  23. package/docs/restacking.md +20 -6
  24. package/docs/seams.md +2 -2
  25. package/docs/setup.md +19 -13
  26. package/docs/spec-standard.md +2 -1
  27. package/docs/ticket-dependencies.md +26 -8
  28. package/docs/ticket-migration.md +70 -7
  29. package/docs/ticket-sources.md +82 -5
  30. package/docs/tickets.md +34 -2
  31. package/docs/troubleshooting.md +2 -2
  32. package/docs/verification.md +5 -5
  33. package/docs/website.md +8 -0
  34. package/docs/workspaces.md +1 -1
  35. package/package.json +5 -4
  36. package/packages/core/src/accounts/hosted-readiness.ts +21 -0
  37. package/packages/core/src/cli/attention.ts +1 -0
  38. package/packages/core/src/cli/build-control.ts +36 -12
  39. package/packages/core/src/cli/connect.ts +9 -18
  40. package/packages/core/src/cli/context.ts +44 -0
  41. package/packages/core/src/cli/dashboard/configuration.ts +11 -0
  42. package/packages/core/src/cli/dashboard/model.ts +40 -10
  43. package/packages/core/src/cli/dashboard/queue.ts +21 -0
  44. package/packages/core/src/cli/dashboard/render.ts +40 -22
  45. package/packages/core/src/cli/dispatch-frontend.ts +63 -30
  46. package/packages/core/src/cli/dispatch-mode.ts +35 -16
  47. package/packages/core/src/cli/dispatch-repository-feed.ts +40 -11
  48. package/packages/core/src/cli/dispatch.ts +154 -47
  49. package/packages/core/src/cli/help.ts +59 -22
  50. package/packages/core/src/cli/init-hosted-accounts.ts +54 -0
  51. package/packages/core/src/cli/init-readiness-remote.ts +16 -3
  52. package/packages/core/src/cli/init-validation.ts +144 -42
  53. package/packages/core/src/cli/issue-links.ts +67 -0
  54. package/packages/core/src/cli/main.ts +32 -15
  55. package/packages/core/src/cli/pr-summary-publication.ts +38 -7
  56. package/packages/core/src/cli/publication-settlement.ts +36 -24
  57. package/packages/core/src/cli/repository-status.ts +201 -49
  58. package/packages/core/src/cli/status.ts +41 -3
  59. package/packages/core/src/cli/terminals.ts +37 -2
  60. package/packages/core/src/cli/ticket-editor.ts +20 -6
  61. package/packages/core/src/cli/ticket.ts +264 -46
  62. package/packages/core/src/cli/triage-count-sampler.ts +59 -0
  63. package/packages/core/src/cli/watch.ts +4 -0
  64. package/packages/core/src/config/live.ts +45 -4
  65. package/packages/core/src/config/load.ts +9 -0
  66. package/packages/core/src/config/pipeline-source.ts +6 -2
  67. package/packages/core/src/config/ready-assignee.ts +86 -0
  68. package/packages/core/src/config/schema.ts +164 -36
  69. package/packages/core/src/distribution-packaging.ts +1 -1
  70. package/packages/core/src/events/catalog.ts +5 -0
  71. package/packages/core/src/events/compatibility.ts +2 -1
  72. package/packages/core/src/events/payloads.ts +70 -0
  73. package/packages/core/src/events/policy.ts +8 -0
  74. package/packages/core/src/events/repository.ts +91 -28
  75. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +41 -0
  76. package/packages/core/src/kernel/build-identity.ts +12 -0
  77. package/packages/core/src/kernel/change-presence.ts +139 -6
  78. package/packages/core/src/kernel/dispatch-capacity.ts +80 -14
  79. package/packages/core/src/kernel/dispatch-status.ts +10 -1
  80. package/packages/core/src/kernel/dispatch-upgrade.ts +104 -5
  81. package/packages/core/src/kernel/engine.ts +4 -2
  82. package/packages/core/src/kernel/finalize-pr-rejection.ts +95 -0
  83. package/packages/core/src/kernel/generators/repository-journals.ts +15 -1
  84. package/packages/core/src/kernel/harvest.ts +4 -1
  85. package/packages/core/src/kernel/integration-completeness.ts +104 -17
  86. package/packages/core/src/kernel/integration-landing.ts +17 -0
  87. package/packages/core/src/kernel/integration-state.ts +17 -0
  88. package/packages/core/src/kernel/issue-keywords.ts +202 -0
  89. package/packages/core/src/kernel/janitor-failure.ts +11 -0
  90. package/packages/core/src/kernel/kernel-git.ts +87 -0
  91. package/packages/core/src/kernel/landing-readiness.ts +25 -17
  92. package/packages/core/src/kernel/log-index.ts +1 -1
  93. package/packages/core/src/kernel/node-issue-links.ts +142 -0
  94. package/packages/core/src/kernel/node-parent.ts +6 -0
  95. package/packages/core/src/kernel/node-restack-transaction.ts +2 -0
  96. package/packages/core/src/kernel/pr-summary.ts +24 -0
  97. package/packages/core/src/kernel/read-deferrals.ts +75 -12
  98. package/packages/core/src/kernel/read-facts.ts +32 -1
  99. package/packages/core/src/kernel/reconcile-restack.ts +1 -1
  100. package/packages/core/src/kernel/reducer-registry.ts +31 -1
  101. package/packages/core/src/kernel/reducer.ts +90 -2
  102. package/packages/core/src/kernel/ticket-preparation.ts +12 -0
  103. package/packages/core/src/kernel/verify-budget.ts +43 -0
  104. package/packages/core/src/operator/index.ts +3 -1
  105. package/packages/core/src/operator/presentation.ts +3 -0
  106. package/packages/core/src/operator/protocol.ts +38 -2
  107. package/packages/core/src/operator/query.ts +66 -29
  108. package/packages/core/src/operator/registry.ts +47 -26
  109. package/packages/core/src/operator/tickets.ts +77 -10
  110. package/packages/core/src/plugin-sdk/index.ts +13 -0
  111. package/packages/core/src/plugins/manifest.ts +1 -1
  112. package/packages/core/src/plugins/registry.ts +1 -0
  113. package/packages/core/src/ports/forge/contract.ts +69 -0
  114. package/packages/core/src/ports/forge/create.ts +5 -0
  115. package/packages/core/src/ports/forge/fake.ts +184 -3
  116. package/packages/core/src/ports/forge/git-rewrite.ts +4 -2
  117. package/packages/core/src/ports/forge/github.ts +175 -38
  118. package/packages/core/src/ports/forge/local-git.ts +14 -0
  119. package/packages/core/src/ports/forge/readiness.ts +10 -0
  120. package/packages/core/src/ports/forge-failure.ts +43 -0
  121. package/packages/core/src/ports/runner/claude.ts +7 -1
  122. package/packages/core/src/ports/runner/codex.ts +7 -2
  123. package/packages/core/src/ports/runner/native-cli-fixture.ts +2 -1
  124. package/packages/core/src/ports/runner/pi.ts +7 -1
  125. package/packages/core/src/ports/runner/runtime.ts +3 -0
  126. package/packages/core/src/ports/runner/start-prompt.ts +37 -0
  127. package/packages/core/src/ports/tickets/contract.ts +320 -7
  128. package/packages/core/src/ports/tickets/create.ts +72 -17
  129. package/packages/core/src/ports/tickets/fake.ts +18 -0
  130. package/packages/core/src/ports/tickets/file-codec.ts +20 -0
  131. package/packages/core/src/ports/tickets/file.ts +43 -1
  132. package/packages/core/src/ports/tickets/github-fake.ts +1123 -0
  133. package/packages/core/src/ports/tickets/github-issues.ts +431 -0
  134. package/packages/core/src/ports/tickets/github-validators.ts +194 -0
  135. package/packages/core/src/ports/tickets/github.ts +2029 -0
  136. package/packages/core/src/ports/tickets/hosted-index.ts +34 -3
  137. package/packages/core/src/ports/tickets/issue-links.ts +86 -0
  138. package/packages/core/src/ports/tickets/lifecycle.ts +60 -4
  139. package/packages/core/src/ports/tickets/linear.ts +66 -6
  140. package/packages/core/src/ports/tickets/migration.ts +290 -68
  141. package/packages/core/src/ports/tickets/operations.ts +98 -12
  142. package/packages/core/src/ports/tickets/priority.ts +55 -0
  143. package/packages/core/src/ports/tickets/remote-protocol.ts +195 -29
  144. package/packages/core/src/ports/tickets/remote.ts +188 -13
  145. package/packages/core/src/ports/tickets/revision.ts +22 -1
  146. package/packages/core/src/ports/tickets/states.ts +43 -0
  147. package/packages/core/src/ports/tickets/tree.ts +58 -12
  148. package/packages/core/src/ports/tickets/update.ts +12 -2
  149. package/packages/core/src/ports/types.ts +194 -11
  150. package/packages/core/src/ports/workspace/commit-preservation.ts +135 -13
  151. package/packages/core/src/ports/workspace/git-worktree.ts +3 -1
  152. package/packages/core/src/ports/workspace/vercel-sandbox.ts +132 -27
  153. package/packages/core/src/processes/build-runner.ts +99 -21
  154. package/packages/core/src/processes/dispatcher.ts +1080 -533
  155. package/packages/core/src/processes/execution-no-progress.ts +5 -0
  156. package/packages/core/src/processes/harvest-runner.ts +17 -8
  157. package/packages/core/src/processes/harvest.ts +5 -8
  158. package/packages/core/src/processes/tick-publication.ts +12 -2
  159. package/packages/core/src/processes/ticket-scan-schedule.ts +78 -0
  160. package/packages/core/src/processes/workspace-preservation.ts +37 -2
  161. package/packages/core/src/runtime-identity.ts +10 -2
  162. package/packages/core/src/scenarios/harness.ts +4 -0
  163. package/packages/core/src/store/adapter.ts +7 -1
  164. package/packages/core/src/store/contract.ts +83 -2
  165. package/packages/core/src/store/digest.ts +32 -1
  166. package/packages/core/src/store/reducer-value-shapes.json +4187 -3391
  167. package/packages/core/src/store/remote/protocol.ts +2 -0
  168. package/packages/core/src/store/remote/released-clients.json +22 -0
  169. package/packages/core/src/store/remote/released-reducer-values/1.5.0.json +26924 -0
  170. package/packages/core/src/store/remote/released-reducer-values/index.ts +2 -0
  171. package/packages/core/src/store/repo-state-events.ts +2 -0
  172. package/packages/core/src/store/types.ts +5 -0
  173. package/packages/core/src/testing/dispatch-read-budget.ts +729 -123
  174. package/packages/core/src/testing/index.ts +18 -1
  175. package/packages/core/src/testing/presence-history.ts +79 -0
  176. package/packages/core/src/testing/reduced-read-budget.ts +1197 -31
  177. package/packages/core/src/testing/upgrade-generations.ts +77 -0
  178. package/skills/code-review/SKILL.md +4 -3
  179. package/skills/finalize/SKILL.md +18 -2
  180. package/skills/guide/SKILL.md +382 -139
  181. package/skills/guide/references/accounts.md +9 -0
  182. package/skills/guide/references/integration-tree.md +91 -28
  183. package/skills/guide/references/merge-policy.md +28 -9
  184. package/skills/guide/references/operator-api.md +43 -18
  185. package/skills/guide/references/plugin-authoring.md +85 -8
  186. package/skills/guide/references/remote-store-protocol.md +25 -7
  187. package/skills/guide/references/remote-ticket-protocol.md +126 -12
  188. package/skills/guide/references/restacking.md +20 -6
  189. package/skills/guide/references/setup.md +19 -13
  190. package/skills/guide/references/spec-standard.md +2 -1
  191. package/skills/guide/references/ticket-dependencies.md +26 -8
  192. package/skills/guide/references/ticket-migration.md +70 -7
  193. package/skills/harvest-review/SKILL.md +4 -3
  194. package/skills/implement/SKILL.md +20 -1
  195. package/skills/operate/SKILL.md +103 -28
  196. package/skills/plan/SKILL.md +1 -1
  197. package/skills/reconcile/SKILL.md +5 -4
  198. package/skills/spec/SKILL.md +15 -2
  199. package/skills/tickets/SKILL.md +114 -37
  200. package/templates/autobuild.toml +5 -0
  201. package/templates/hosted-service/package.json +2 -2
@@ -1,7 +1,7 @@
1
1
  // Client half of the hosted ticket source (SPEC §13): the TicketSource a
2
2
  // local CLI drives against a hosted deployment. The HTTP server half ships
3
3
  // in `@defrex/autobuild-hosted-store-service`.
4
- export { HostedTicketSource } from './remote'
4
+ export { HostedTicketSource, hostedTicketPolicy } from './remote'
5
5
  export type { HostedTicketFetch, HostedTicketSourceOptions } from './remote'
6
6
  export {
7
7
  HOSTED_TICKET_OPERATIONS,
@@ -9,6 +9,17 @@ export {
9
9
  TICKET_TREE_CAPABILITY,
10
10
  TICKET_COMMENTS_HEADER,
11
11
  TICKET_COMMENTS_CAPABILITY,
12
+ TICKET_ISSUE_LINKS_HEADER,
13
+ TICKET_ISSUE_LINKS_CAPABILITY,
14
+ TICKET_PRIORITY_HEADER,
15
+ TICKET_PRIORITY_CAPABILITY,
16
+ TICKET_DEPENDENCY_STATE_HEADER,
17
+ TICKET_DEPENDENCY_STATE_CAPABILITY,
18
+ TICKET_POLICY_ROLES_HEADER,
19
+ TICKET_POLICY_ROLES_CAPABILITY,
20
+ TICKET_TARGETING_HEADER,
21
+ TICKET_TARGETING_CAPABILITY,
22
+ TICKET_TARGETING_PROBE_NODE,
12
23
  hostedTicketContextSchema,
13
24
  hostedTicketRequestSchemas,
14
25
  TICKET_REVISIONS_HEADER,
@@ -21,10 +32,30 @@ export {
21
32
  TicketRevisionUnsupported,
22
33
  mutateTicketIfRevision,
23
34
  } from './revision'
35
+ export { TicketIssueLinksUnsupported, refuseUnsupportedIssueLinks } from './issue-links'
36
+ export {
37
+ TicketPriorityUnsupported,
38
+ refuseUnsupportedPriority,
39
+ withoutUnsupportedPriority,
40
+ ticketPriority,
41
+ urgentFirst,
42
+ type TicketPriority,
43
+ } from './priority'
24
44
 
25
- export { hostedTicketPolicySchema, type HostedTicketPolicy } from './remote-protocol'
45
+ export {
46
+ hostedTicketPolicySchema,
47
+ projectTicketPolicy,
48
+ publishedTicketPolicy,
49
+ type HostedTicketPolicy,
50
+ } from './remote-protocol'
26
51
  export { ticketLifecycle } from './lifecycle'
27
52
 
28
- export { orderStates, resolveState, validateStateName, validateStatePurpose } from './states'
53
+ export {
54
+ matchStateNames,
55
+ orderStates,
56
+ resolveState,
57
+ validateStateName,
58
+ validateStatePurpose,
59
+ } from './states'
29
60
 
30
61
  export { hostedRequestedIds, MAX_TICKET_NUMBER } from './requested-id'
@@ -0,0 +1,86 @@
1
+ import { z } from 'zod'
2
+ import { expandIssues, type IssueSink } from '../../zod-issues'
3
+ import type { IssueLink, IssueLinkIntent, TicketSource } from '../types'
4
+
5
+ export type { IssueLink, IssueLinkIntent } from '../types'
6
+
7
+ /** Input form: intent is optional and defaults to `closes` in `normalizeIssueLinks`. */
8
+ export interface IssueLinkInput {
9
+ issue: number
10
+ intent?: IssueLinkIntent
11
+ }
12
+
13
+ export const issueLinkInputSchema = z.strictObject({
14
+ issue: z.number().int().positive().max(Number.MAX_SAFE_INTEGER),
15
+ intent: z.enum(['closes', 'refs']).optional(),
16
+ })
17
+
18
+ export function refineUniqueIssues(links: ReadonlyArray<{ issue: number }>, ctx: IssueSink): void {
19
+ const seen = new Set<number>()
20
+ for (const link of links) {
21
+ if (seen.has(link.issue))
22
+ ctx.addIssue({ code: 'custom', message: `duplicate issue #${link.issue}` })
23
+ seen.add(link.issue)
24
+ }
25
+ }
26
+
27
+ export const issueLinksInputSchema = z.array(issueLinkInputSchema).superRefine(refineUniqueIssues)
28
+
29
+ /** Validate, default intent to `closes`, and sort ascending by issue number. */
30
+ export function normalizeIssueLinks(input: ReadonlyArray<IssueLinkInput>): IssueLink[] {
31
+ const result = issueLinksInputSchema.safeParse(input)
32
+ if (!result.success)
33
+ throw new Error(
34
+ `invalid issue links — ${expandIssues(result.error.issues)
35
+ .map((issue) => {
36
+ const path = issue.path.join('.')
37
+ return path === '' ? issue.message : `${path}: ${issue.message}`
38
+ })
39
+ .join('; ')}`,
40
+ )
41
+ return result.data
42
+ .map((link) => ({ issue: link.issue, intent: link.intent ?? 'closes' }))
43
+ .sort((a, b) => a.issue - b.issue)
44
+ }
45
+
46
+ /** Parse a CLI comma list such as `824,820:refs`; an empty value yields `[]`. */
47
+ export function parseIssueLinkList(value: string, flag = '--issues'): IssueLink[] {
48
+ const links: IssueLinkInput[] = []
49
+ for (const item of value.split(',').map((part) => part.trim())) {
50
+ if (!item) continue
51
+ const match = /^#?(\d+)(?::(closes|refs))?$/.exec(item)
52
+ if (!match)
53
+ throw new Error(`${flag}: invalid issue link "${item}" — expected N, N:closes or N:refs`)
54
+ links.push({
55
+ issue: Number(match[1]),
56
+ ...(match[2] !== undefined ? { intent: match[2] as IssueLinkIntent } : {}),
57
+ })
58
+ }
59
+ try {
60
+ return normalizeIssueLinks(links)
61
+ } catch (error) {
62
+ throw new Error(`${flag}: ${error instanceof Error ? error.message : String(error)}`)
63
+ }
64
+ }
65
+
66
+ /** Human form, e.g. `closes #824, refs #820`. */
67
+ export function formatIssueLinks(links: ReadonlyArray<IssueLink>): string {
68
+ return links.map((link) => `${link.intent} #${link.issue}`).join(', ')
69
+ }
70
+
71
+ /** A write naming issue links reached a source that cannot store them. */
72
+ export class TicketIssueLinksUnsupported extends Error {
73
+ constructor(readonly source: string) {
74
+ super(`${source} ticket source cannot store issue links; nothing was written`)
75
+ this.name = 'TicketIssueLinksUnsupported'
76
+ }
77
+ }
78
+
79
+ /** Refuse before any write: a present `issueLinks` (including `[]`) needs the capability. */
80
+ export function refuseUnsupportedIssueLinks(
81
+ source: Pick<TicketSource, 'name' | 'issueLinks'>,
82
+ patch: { issueLinks?: unknown },
83
+ ): void {
84
+ if (patch.issueLinks !== undefined && !source.issueLinks)
85
+ throw new TicketIssueLinksUnsupported(source.name)
86
+ }
@@ -1,9 +1,24 @@
1
1
  import type { TicketsConfig } from '../../config/schema'
2
2
 
3
- /** Resolve names without changing persisted ticket states or their spelling. */
4
- export function ticketLifecycle(tickets: TicketsConfig, legacyHosted = false) {
5
- const hosted = tickets.source === 'hosted'
6
- const old = tickets.source === 'linear' || (hosted && legacyHosted)
3
+ /** Resolve names without changing persisted ticket states or their spelling.
4
+ * `backlog` and `rejected` are optional roles: undefined when unset, with no
5
+ * default, so an unconfigured role never matches a state of the same name. */
6
+ export interface TicketLifecycle {
7
+ triage: string
8
+ ready: string
9
+ doing: string
10
+ done: string
11
+ create: string
12
+ proposal: string
13
+ backlog?: string
14
+ rejected?: string
15
+ }
16
+
17
+ export function ticketLifecycle(tickets: TicketsConfig, legacyHosted = false): TicketLifecycle {
18
+ // GitHub states are `autobuild:<state>` labels, so its defaults share the
19
+ // hosted source's lowercase spelling.
20
+ const hosted = tickets.source === 'hosted' || tickets.source === 'github'
21
+ const old = tickets.source === 'linear' || (tickets.source === 'hosted' && legacyHosted)
7
22
  const triage = tickets.triageState ?? (old ? 'Backlog' : hosted ? 'triage' : 'Triage')
8
23
  return {
9
24
  triage,
@@ -14,5 +29,46 @@ export function ticketLifecycle(tickets: TicketsConfig, legacyHosted = false) {
14
29
  done: tickets.doneState ?? (old ? 'Done' : hosted ? 'done' : 'Done'),
15
30
  create: tickets.createState ?? triage,
16
31
  proposal: tickets.proposalState ?? triage,
32
+ ...(tickets.backlogState === undefined ? {} : { backlog: tickets.backlogState }),
33
+ ...(tickets.rejectedState === undefined ? {} : { rejected: tickets.rejectedState }),
17
34
  }
18
35
  }
36
+
37
+ /** Whether a native state is the one a role names. Case-insensitive, as
38
+ * `ab ticket states` and the file source already match state names; false
39
+ * when either side is absent, so an unset role holds nothing. */
40
+ export function holdsState(state: string | undefined, name: string | undefined): boolean {
41
+ return (
42
+ state !== undefined &&
43
+ name !== undefined &&
44
+ state.trim().toLowerCase() === name.trim().toLowerCase()
45
+ )
46
+ }
47
+
48
+ /** Roles a ticket can be moved into by role rather than by native name. */
49
+ export const TICKET_MOVE_ROLES = ['triage', 'ready', 'backlog', 'rejected'] as const
50
+ export type TicketMoveRole = (typeof TICKET_MOVE_ROLES)[number]
51
+
52
+ /** Every lifecycle role, in import priority: a state holding several roles
53
+ * lands in the destination's state for the first. */
54
+ export const LIFECYCLE_ROLES = [
55
+ 'done',
56
+ 'rejected',
57
+ 'claimed',
58
+ 'ready',
59
+ 'backlog',
60
+ 'triage',
61
+ 'proposal',
62
+ 'create',
63
+ ] as const
64
+ export type LifecycleRole = (typeof LIFECYCLE_ROLES)[number]
65
+
66
+ /** The native state a role names; `claimed` is the lifecycle's `doing`. */
67
+ export function roleState(lifecycle: TicketLifecycle, role: LifecycleRole): string | undefined {
68
+ return role === 'claimed' ? lifecycle.doing : lifecycle[role]
69
+ }
70
+
71
+ /** The roles native state `name` holds, in `LIFECYCLE_ROLES` order. */
72
+ export function stateRolesIn(lifecycle: TicketLifecycle, name: string): LifecycleRole[] {
73
+ return LIFECYCLE_ROLES.filter((role) => holdsState(name, roleState(lifecycle, role)))
74
+ }
@@ -23,6 +23,7 @@ import type {
23
23
  } from '../types'
24
24
  import { TicketSourceRateLimitError } from '../types'
25
25
  import { validateTicketUpdate } from './update'
26
+ import { refuseUnsupportedIssueLinks } from './issue-links'
26
27
 
27
28
  export const LINEAR_API_URL = 'https://api.linear.app/graphql'
28
29
 
@@ -98,7 +99,7 @@ interface RelationOwner {
98
99
  interface DependencyIssue extends RelationOwner {
99
100
  id?: string
100
101
  identifier: string
101
- state: { type: string } | null
102
+ state: { name?: string; type: string } | null
102
103
  }
103
104
  interface Connection<T> {
104
105
  nodes: T[]
@@ -118,6 +119,8 @@ interface GqlIssue {
118
119
  title: string
119
120
  description: string | null
120
121
  url: string
122
+ /** Linear's 0–4 scale; only 1 (Urgent) maps to `urgent`. */
123
+ priority?: number
121
124
  state: { name: string; type: string } | null
122
125
  labels: { nodes: Array<{ name: string }>; pageInfo?: PageInfo }
123
126
  comments?: Connection<{ body: string; createdAt: string; user: { name: string } | null }>
@@ -218,7 +221,7 @@ const METADATA_FIELDS =
218
221
  'team { key } parent { id identifier team { key } labels(first: 50) { nodes { name } pageInfo { hasNextPage endCursor } } }'
219
222
 
220
223
  const ISSUE_FIELDS =
221
- `${METADATA_FIELDS} id identifier title description url state { name type } labels(first: 50) { nodes { name } pageInfo { hasNextPage endCursor } } ` +
224
+ `${METADATA_FIELDS} id identifier title description url priority state { name type } labels(first: 50) { nodes { name } pageInfo { hasNextPage endCursor } } ` +
222
225
  `inverseRelations(first: ${LINEAR_RELATION_PAGE_SIZE}) { nodes { id type issue { id identifier } } }`
223
226
 
224
227
  // Linear computes complexity from requested bounds, not returned counts:
@@ -235,10 +238,10 @@ const PAGE_INFO = 'pageInfo { hasNextPage endCursor }'
235
238
  const DEPENDENCY_RELATIONS = 'type issue { identifier }'
236
239
  const READY_RELATIONS = 'id type issue { id identifier state { type } }'
237
240
  const relationFields = (ready: boolean) => (ready ? READY_RELATIONS : DEPENDENCY_RELATIONS)
238
- const dependencyFields = `id identifier state { type } inverseRelations(first: ${RELATION_PAGE_SIZE}) { nodes { ${DEPENDENCY_RELATIONS} } ${PAGE_INFO} }`
241
+ const dependencyFields = `id identifier state { name type } inverseRelations(first: ${RELATION_PAGE_SIZE}) { nodes { ${DEPENDENCY_RELATIONS} } ${PAGE_INFO} }`
239
242
  const DEPENDENCY_QUERY = `query Dependencies($filter: IssueFilter!, $first: Int!, $after: String) { issues(filter: $filter, first: $first, after: $after, includeArchived: true) { nodes { ${dependencyFields} } ${PAGE_INFO} } }`
240
243
  const DEPENDENCY_FALLBACK_QUERY = `query Dependency($id: String!) { issue(id: $id) { ${dependencyFields} } }`
241
- const READY_FIELDS = `${METADATA_FIELDS} id identifier title description url state { name type } labels(first: ${READY_LABEL_PAGE_SIZE}) { nodes { name } ${PAGE_INFO} } inverseRelations(first: ${RELATION_PAGE_SIZE}) { nodes { ${READY_RELATIONS} } ${PAGE_INFO} }`
244
+ const READY_FIELDS = `${METADATA_FIELDS} id identifier title description url priority state { name type } labels(first: ${READY_LABEL_PAGE_SIZE}) { nodes { name } ${PAGE_INFO} } inverseRelations(first: ${RELATION_PAGE_SIZE}) { nodes { ${READY_RELATIONS} } ${PAGE_INFO} }`
242
245
  const LIST_READY_QUERY = `query ListReady($filter: IssueFilter!, $first: Int!, $after: String) { issues(filter: $filter, first: $first, after: $after) { nodes { ${READY_FIELDS} } ${PAGE_INFO} } }`
243
246
 
244
247
  /** Refuse partial success and cursor loops on every connection independently. */
@@ -251,9 +254,10 @@ function nextCursor(info: PageInfo | undefined, seen: Set<string>): string | nul
251
254
  return cursor
252
255
  }
253
256
 
254
- const DETAIL_FIELDS = `${METADATA_FIELDS} id identifier title description url state { name type } labels(first: 100) { nodes { name } ${PAGE_INFO} } inverseRelations(first: 100) { nodes { ${READY_RELATIONS} } ${PAGE_INFO} } comments(first: 100, includeArchived: true, orderBy: createdAt) { nodes { body createdAt user { name } } ${PAGE_INFO} }`
257
+ const DETAIL_FIELDS = `${METADATA_FIELDS} id identifier title description url priority state { name type } labels(first: 100) { nodes { name } ${PAGE_INFO} } inverseRelations(first: 100) { nodes { ${READY_RELATIONS} } ${PAGE_INFO} } comments(first: 100, includeArchived: true, orderBy: createdAt) { nodes { body createdAt user { name } } ${PAGE_INFO} }`
255
258
  const GET_DETAIL_QUERY = `query GetIssue($id: String!) { issue(id: $id) { ${DETAIL_FIELDS} } }`
256
259
  const GET_ISSUE_QUERY = `query GetIssue($id: String!) { issue(id: $id) { ${ISSUE_FIELDS} } }`
260
+ const ISSUE_PRIORITY_QUERY = `query IssuePriority($id: String!) { issue(id: $id) { id priority } }`
257
261
  const RESOLVE_ISSUE_QUERY = `query ResolveIssue($id: String!) { issue(id: $id) { id } }`
258
262
  const ISSUE_STATE_QUERY = `query IssueState($id: String!) { issue(id: $id) { id state { name type } } }`
259
263
  const TEAM_INFO_QUERY = `query TeamInfo($teamKey: String!) { teams(filter: { key: { eq: $teamKey } }) { nodes { id states { nodes { id name } } labels { nodes { id name } } } } }`
@@ -265,6 +269,9 @@ const CREATE_ISSUE_MUTATION = `mutation CreateIssue($input: IssueCreateInput!) {
265
269
  const UPDATE_ISSUE_MUTATION = `mutation UpdateIssue($id: String!, $input: IssueUpdateInput!) { issueUpdate(id: $id, input: $input) { success } }`
266
270
  const CREATE_RELATION_MUTATION = `mutation CreateRelation($issueId: String!, $relatedIssueId: String!) { issueRelationCreate(input: { issueId: $issueId, relatedIssueId: $relatedIssueId, type: blocks }) { success } }`
267
271
  const DELETE_RELATION_MUTATION = `mutation DeleteRelation($id: String!) { issueRelationDelete(id: $id) { success } }`
272
+ // Linear priority: 0 = No priority, 1 = Urgent, 2 = High, 3 = Medium, 4 = Low.
273
+ const LINEAR_NO_PRIORITY = 0
274
+ const LINEAR_URGENT = 1
268
275
  const linearReservedIssueIdSchema = z.uuidv4()
269
276
 
270
277
  type ReadQuery = <T>(
@@ -303,6 +310,8 @@ async function abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {
303
310
  export class LinearTicketSource implements TicketSource {
304
311
  readonly name = 'linear'
305
312
  readonly integrationTree = true
313
+ readonly priority = true
314
+ readonly readyAssignee = true
306
315
 
307
316
  async readExportSnapshot(opts: ExportSnapshotOptions = {}): Promise<Ticket[]> {
308
317
  let completed = 0
@@ -467,6 +476,19 @@ export class LinearTicketSource implements TicketSource {
467
476
  return data.users.nodes[0] ?? null
468
477
  }
469
478
 
479
+ /** `ab init --validate`: who a `readyAssignee` value names. */
480
+ async describeReadyAssignee(assignee: string): Promise<string> {
481
+ const user = await this.resolveAssignee(assignee)
482
+ if (user === null) {
483
+ throw new Error(
484
+ assignee === 'me'
485
+ ? '[tickets].readyAssignee = "me" but LINEAR_API_KEY did not resolve to a Linear user — check the key'
486
+ : `[tickets].readyAssignee "${assignee}" matches no user in the Linear workspace — check the address or use "me"`,
487
+ )
488
+ }
489
+ return `${assignee} → ${user.name} <${user.email}>`
490
+ }
491
+
470
492
  async listReady(criteria: {
471
493
  labels?: string[]
472
494
  state?: string
@@ -667,6 +689,8 @@ export class LinearTicketSource implements TicketSource {
667
689
  }
668
690
 
669
691
  async create(draft: TicketDraft, opts: TicketCreateOptions = {}): Promise<Ticket> {
692
+ // Linear links GitHub issues natively through its own integration.
693
+ refuseUnsupportedIssueLinks(this, draft)
670
694
  if (opts.requestedId !== undefined) throw new Error('linear does not support requested ids')
671
695
  const reservedId = opts.idempotencyKey
672
696
  if (reservedId !== undefined && !linearReservedIssueIdSchema.safeParse(reservedId).success) {
@@ -701,6 +725,8 @@ export class LinearTicketSource implements TicketSource {
701
725
  if (canonicalTarget != null)
702
726
  input.parentId = await this.resolveIssueId('create', canonicalTarget)
703
727
  if (reservedId !== undefined) input.id = reservedId
728
+ // `normal` needs nothing: Linear creates issues with No priority.
729
+ if (draft.priority === 'urgent') input.priority = LINEAR_URGENT
704
730
 
705
731
  let issue: GqlIssue | null = null
706
732
  let createError: unknown
@@ -733,6 +759,7 @@ export class LinearTicketSource implements TicketSource {
733
759
  }
734
760
 
735
761
  async update(id: string, patch: TicketUpdate): Promise<void> {
762
+ refuseUnsupportedIssueLinks(this, patch)
736
763
  const validated = validateTicketUpdate(patch)
737
764
  if (
738
765
  validated.kind !== undefined ||
@@ -750,8 +777,16 @@ export class LinearTicketSource implements TicketSource {
750
777
  if (validated.target !== undefined || (current.kind === 'node' && effectiveKind === 'work'))
751
778
  validated.target = await this.validateTree(id, { ...validated, kind: effectiveKind })
752
779
  }
753
- const issueId = await this.resolveIssueId('update', id)
754
780
  const input: Record<string, unknown> = {}
781
+ if (validated.priority === 'urgent') input.priority = LINEAR_URGENT
782
+ // `normal` clears only Urgent; High, Medium or Low set by a person stays.
783
+ // A person setting Urgent between this read and the write keeps it.
784
+ else if (
785
+ validated.priority === 'normal' &&
786
+ (await this.currentPriority('update', id)) === LINEAR_URGENT
787
+ )
788
+ input.priority = LINEAR_NO_PRIORITY
789
+ const issueId = await this.resolveIssueId('update', id)
755
790
 
756
791
  if (validated.target === null) input.parentId = null
757
792
  else if (validated.target !== undefined)
@@ -768,6 +803,9 @@ export class LinearTicketSource implements TicketSource {
768
803
  )
769
804
  }
770
805
  }
806
+ // Only a priority-only `normal` patch on a non-urgent issue gets here
807
+ // empty; there is nothing to write.
808
+ if (Object.keys(input).length === 0) return
771
809
 
772
810
  const data = await this.gql<{ issueUpdate: { success: boolean } }>(
773
811
  'update',
@@ -918,6 +956,7 @@ export class LinearTicketSource implements TicketSource {
918
956
  id,
919
957
  exists: !!issue,
920
958
  resolved: !!issue && RESOLVED_STATE_TYPES.has(issue.state?.type ?? ''),
959
+ ...(issue?.state?.name ? { state: issue.state.name } : {}),
921
960
  blockedBy: issue ? blockersOf(issue) : [],
922
961
  }
923
962
  })
@@ -1204,6 +1243,24 @@ export class LinearTicketSource implements TicketSource {
1204
1243
  return payload.data
1205
1244
  }
1206
1245
 
1246
+ /** Read an issue's Linear priority, caching its UUID for the write. */
1247
+ private async currentPriority(operation: string, id: string): Promise<number | undefined> {
1248
+ let data: { issue: { id: string; priority?: number } | null }
1249
+ try {
1250
+ data = await this.gql<{ issue: { id: string; priority?: number } | null }>(
1251
+ operation,
1252
+ ISSUE_PRIORITY_QUERY,
1253
+ { id },
1254
+ )
1255
+ } catch (error) {
1256
+ if (isEntityNotFound(error)) throw new Error(`linear ${operation}: unknown ticket "${id}"`)
1257
+ throw error
1258
+ }
1259
+ if (!data.issue) throw new Error(`linear ${operation}: unknown ticket "${id}"`)
1260
+ this.issueIds.set(id, data.issue.id)
1261
+ return data.issue.priority
1262
+ }
1263
+
1207
1264
  private async resolveIssueId(operation: string, id: string): Promise<string> {
1208
1265
  const cached = this.issueIds.get(id)
1209
1266
  if (cached) return cached
@@ -1358,6 +1415,7 @@ export class LinearTicketSource implements TicketSource {
1358
1415
  issue.parent.labels.nodes.some((label) => label.name === 'node')
1359
1416
  ? issue.parent.identifier
1360
1417
  : undefined
1418
+ const priority = issue.priority === LINEAR_URGENT ? ('urgent' as const) : undefined
1361
1419
  return {
1362
1420
  kind,
1363
1421
  ...(target !== undefined ? { target } : {}),
@@ -1369,6 +1427,7 @@ export class LinearTicketSource implements TicketSource {
1369
1427
  labels: issue.labels.nodes.map((label) => label.name),
1370
1428
  state: issue.state?.name,
1371
1429
  blockedBy,
1430
+ priority,
1372
1431
  }),
1373
1432
  ref: {
1374
1433
  source: this.name,
@@ -1383,6 +1442,7 @@ export class LinearTicketSource implements TicketSource {
1383
1442
  resolved: RESOLVED_STATE_TYPES.has(issue.state?.type ?? ''),
1384
1443
  labels: issue.labels.nodes.map((label) => label.name),
1385
1444
  ...(blockedBy.length > 0 ? { blockedBy } : {}),
1445
+ ...(priority ? { priority } : {}),
1386
1446
  }
1387
1447
  }
1388
1448
  }