@defrex/autobuild 1.10.0 → 1.11.1
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/README.md +1 -1
- package/SPEC.md +745 -69
- package/bin/ab-dispatch-generation.ts +3 -1
- package/docs/accounts.md +2 -1
- package/docs/answering-blocked-builds.md +20 -0
- package/docs/architecture.md +165 -29
- package/docs/build-store.md +10 -7
- package/docs/cli.md +33 -1
- package/docs/configuration-overview.md +4 -1
- package/docs/configuration.md +381 -29
- package/docs/dispatcher.md +64 -4
- package/docs/forges.md +7 -0
- package/docs/integration-tree.md +2 -2
- package/docs/operating.md +4 -2
- package/docs/operator-api.md +134 -6
- package/docs/operator-surfaces.md +85 -0
- package/docs/optimization-nodes.md +69 -11
- package/docs/plugin-authoring.md +37 -1
- package/docs/remote-store-protocol.md +94 -13
- package/docs/remote-ticket-protocol.md +40 -0
- package/docs/restacking.md +20 -4
- package/docs/setup.md +14 -2
- package/docs/ticket-dependencies.md +10 -3
- package/docs/ticket-sources.md +24 -2
- package/docs/tickets.md +7 -1
- package/docs/troubleshooting.md +8 -0
- package/docs/verification.md +4 -3
- package/docs/workspaces.md +3 -0
- package/package.json +9 -5
- package/packages/core/src/cli/accounts.ts +7 -2
- package/packages/core/src/cli/attention.ts +3 -1
- package/packages/core/src/cli/binary-direct.ts +2 -0
- package/packages/core/src/cli/binary-lifecycle.ts +2 -1
- package/packages/core/src/cli/build-control.ts +79 -1
- package/packages/core/src/cli/context.ts +4 -0
- package/packages/core/src/cli/dashboard/facts.ts +49 -53
- package/packages/core/src/cli/dashboard/header.ts +111 -0
- package/packages/core/src/cli/dashboard/model.ts +31 -39
- package/packages/core/src/cli/dashboard/objective-row.ts +46 -14
- package/packages/core/src/cli/dashboard/poll.ts +179 -76
- package/packages/core/src/cli/dashboard/render.ts +159 -230
- package/packages/core/src/cli/dashboard/row-text.ts +538 -0
- package/packages/core/src/cli/dashboard/timeline.ts +288 -0
- package/packages/core/src/cli/dashboard/vocabulary.ts +188 -0
- package/packages/core/src/cli/dispatch-args.ts +44 -1
- package/packages/core/src/cli/dispatch-frontend.ts +40 -40
- package/packages/core/src/cli/dispatch-launch-stop.ts +18 -0
- package/packages/core/src/cli/dispatch-launcher.ts +20 -7
- package/packages/core/src/cli/dispatch-web.ts +155 -0
- package/packages/core/src/cli/dispatch.ts +202 -38
- package/packages/core/src/cli/help.ts +87 -3
- package/packages/core/src/cli/init-validation.ts +3 -3
- package/packages/core/src/cli/main.ts +70 -6
- package/packages/core/src/cli/mcp.ts +35 -3
- package/packages/core/src/cli/objective.ts +1 -1
- package/packages/core/src/cli/operator-preference.ts +57 -0
- package/packages/core/src/cli/repo-lease.ts +2 -0
- package/packages/core/src/cli/repo-state.ts +56 -6
- package/packages/core/src/cli/repository-status.ts +105 -49
- package/packages/core/src/cli/status.ts +82 -1
- package/packages/core/src/cli/terminal-restore.ts +32 -7
- package/packages/core/src/cli/ticket.ts +98 -36
- package/packages/core/src/cli/wait.ts +14 -6
- package/packages/core/src/cli/watch.ts +20 -4
- package/packages/core/src/cli/web.ts +139 -0
- package/packages/core/src/config/live.ts +99 -14
- package/packages/core/src/config/load.ts +61 -20
- package/packages/core/src/config/local.ts +212 -0
- package/packages/core/src/config/pipeline-source.ts +2 -0
- package/packages/core/src/config/reference.ts +18 -0
- package/packages/core/src/config/schema.ts +45 -10
- package/packages/core/src/events/catalog.ts +4 -0
- package/packages/core/src/events/payloads.ts +97 -0
- package/packages/core/src/events/policy.ts +10 -0
- package/packages/core/src/events/repository.ts +46 -3
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +381 -30
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/017d8dd5bb6dd80b.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/178e12f1759b7552.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/4932c70a2af97faf.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/536085773daf6b58.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/6bd6275061aaffa5.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/89625654b02e9ac1.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/a98af134be2da8a9.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/d5d5ccca80a893d8.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/f8155387eb5f375c.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/3c27f918b194e3b3.json +465 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/46196d8f0076496a.json +97 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/79f0e0f4ccd43ba2.json +455 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/859c09edac738823.json +1086 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a283c1f3462b1cc1.json +81 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a6e570fb126991ff.json +36 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a8acf22c968b6e3d.json +81 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/ba7b85895bf6698e.json +1153 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/fc66408cde0b6b04.json +637 -0
- package/packages/core/src/kernel/build-roster.ts +230 -0
- package/packages/core/src/kernel/dispatch-capacity.ts +4 -2
- package/packages/core/src/kernel/dispatch-status.ts +139 -0
- package/packages/core/src/kernel/engine.ts +43 -5
- package/packages/core/src/kernel/execution-intervals.ts +61 -0
- package/packages/core/src/kernel/generators/optimization-log.ts +101 -0
- package/packages/core/src/kernel/generators/repository-journals.ts +13 -0
- package/packages/core/src/kernel/leaf-restack-base.ts +33 -7
- package/packages/core/src/kernel/leaf-restack-state.ts +82 -3
- package/packages/core/src/kernel/log-index.ts +19 -0
- package/packages/core/src/kernel/optimization-admission.ts +244 -3
- package/packages/core/src/kernel/optimization.ts +183 -4
- package/packages/core/src/kernel/phase-failures.ts +121 -0
- package/packages/core/src/kernel/published-branch-head.ts +60 -0
- package/packages/core/src/kernel/read-facts.ts +15 -13
- package/packages/core/src/kernel/reducer-registry.ts +27 -8
- package/packages/core/src/kernel/reducer-revisions.ts +27 -0
- package/packages/core/src/kernel/reducer.ts +63 -4
- package/packages/core/src/kernel/rollup.ts +61 -0
- package/packages/core/src/kernel/setup-streak.ts +38 -0
- package/packages/core/src/kernel/step-durations.ts +257 -0
- package/packages/core/src/kernel/target-observation.ts +6 -1
- package/packages/core/src/kernel/target-rewrite-recovery.ts +116 -0
- package/packages/core/src/kernel/verify-diff-base.ts +5 -0
- package/packages/core/src/kernel/verify-stages.ts +13 -3
- package/packages/core/src/ontology.ts +5 -0
- package/packages/core/src/operator/agent-context-serve.ts +631 -0
- package/packages/core/src/operator/annotations.ts +75 -5
- package/packages/core/src/operator/attention.ts +470 -0
- package/packages/core/src/operator/canonical-skills.ts +35 -0
- package/packages/core/src/operator/cli-equivalents.ts +270 -0
- package/packages/core/src/operator/context-prompts.ts +61 -0
- package/packages/core/src/operator/index.ts +49 -1
- package/packages/core/src/operator/landings.ts +28 -19
- package/packages/core/src/operator/optimization.ts +7 -0
- package/packages/core/src/operator/protocol.ts +3 -0
- package/packages/core/src/operator/query.ts +129 -74
- package/packages/core/src/operator/read-view.ts +5 -2
- package/packages/core/src/operator/registry.ts +406 -46
- package/packages/core/src/operator/requests.ts +20 -18
- package/packages/core/src/operator/sandbox-host.ts +107 -11
- package/packages/core/src/operator/sandbox-resolver.ts +253 -0
- package/packages/core/src/operator/sandbox.ts +778 -82
- package/packages/core/src/operator/ticket-asset-list.ts +77 -0
- package/packages/core/src/operator/ticket-assets.ts +167 -0
- package/packages/core/src/operator/tickets.ts +33 -2
- package/packages/core/src/plugin-sdk/index.ts +22 -0
- package/packages/core/src/plugins/manifest.ts +1 -1
- package/packages/core/src/ports/forge/fake.ts +92 -1
- package/packages/core/src/ports/forge/git-blob.ts +15 -0
- package/packages/core/src/ports/forge/github.ts +82 -0
- package/packages/core/src/ports/forge/local-git.ts +61 -1
- package/packages/core/src/ports/forge-failure.ts +24 -0
- package/packages/core/src/ports/runner/antigravity-test.fixture.ts +149 -0
- package/packages/core/src/ports/runner/claude-test.fixture.ts +71 -0
- package/packages/core/src/ports/runner/codex-test.fixture.ts +63 -0
- package/packages/core/src/ports/tickets/contract.ts +71 -0
- package/packages/core/src/ports/tickets/fake.ts +11 -0
- package/packages/core/src/ports/tickets/file.ts +18 -0
- package/packages/core/src/ports/tickets/github-fake.ts +136 -4
- package/packages/core/src/ports/tickets/github.ts +387 -30
- package/packages/core/src/ports/tickets/hosted-index.ts +10 -0
- package/packages/core/src/ports/tickets/linear.ts +130 -0
- package/packages/core/src/ports/tickets/priority.ts +9 -0
- package/packages/core/src/ports/tickets/remote-protocol.ts +34 -0
- package/packages/core/src/ports/tickets/remote.ts +70 -6
- package/packages/core/src/ports/tickets/search.ts +139 -0
- package/packages/core/src/ports/types.ts +104 -2
- package/packages/core/src/ports/workspace/builtin-capabilities.ts +9 -0
- package/packages/core/src/ports/workspace/create.ts +15 -1
- package/packages/core/src/ports/workspace/fake.ts +340 -12
- package/packages/core/src/ports/workspace/git-worktree.ts +833 -42
- package/packages/core/src/ports/workspace/operator-sandbox.ts +129 -12
- package/packages/core/src/ports/workspace/process-group.ts +48 -0
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +1260 -123
- package/packages/core/src/processes/agent-context-assemble.ts +576 -0
- package/packages/core/src/processes/agent-context.ts +469 -0
- package/packages/core/src/processes/build-runner.ts +387 -236
- package/packages/core/src/processes/dispatcher.ts +211 -55
- package/packages/core/src/processes/execution-no-progress.ts +4 -0
- package/packages/core/src/processes/optimization-activity.ts +120 -0
- package/packages/core/src/processes/phase-failure-budget.ts +1 -0
- package/packages/core/src/processes/repo-view.ts +78 -2
- package/packages/core/src/processes/sandbox-state.ts +53 -6
- package/packages/core/src/processes/tick-publication.ts +2 -0
- package/packages/core/src/processes/workspace-preparation.ts +76 -0
- package/packages/core/src/scenarios/harness.ts +0 -5
- package/packages/core/src/store/account-redaction.ts +1 -0
- package/packages/core/src/store/adapter.ts +52 -1
- package/packages/core/src/store/agent-context.ts +432 -0
- package/packages/core/src/store/build-feed.ts +291 -0
- package/packages/core/src/store/build-scope.ts +19 -1
- package/packages/core/src/store/contract.ts +911 -14
- package/packages/core/src/store/digest.ts +19 -6
- package/packages/core/src/store/histogram.ts +113 -0
- package/packages/core/src/store/local/schema.ts +62 -0
- package/packages/core/src/store/local/store.ts +628 -22
- package/packages/core/src/store/local/ticket-asset-heads.ts +23 -0
- package/packages/core/src/store/memory.ts +461 -69
- package/packages/core/src/store/open-builds.ts +19 -0
- package/packages/core/src/store/phase-session.ts +32 -4
- package/packages/core/src/store/reduced-reads.ts +14 -4
- package/packages/core/src/store/reducer-value-shapes.json +7027 -5751
- package/packages/core/src/store/reducer-values.ts +6 -1
- package/packages/core/src/store/remote/client.ts +208 -2
- package/packages/core/src/store/remote/compatibility.ts +1 -1
- package/packages/core/src/store/remote/index.ts +33 -0
- package/packages/core/src/store/remote/protocol.ts +69 -0
- package/packages/core/src/store/remote/released-clients.json +22 -0
- package/packages/core/src/store/remote/released-reducer-values/1.11.0.json +35680 -0
- package/packages/core/src/store/remote/released-reducer-values/1.11.1.json +35718 -0
- package/packages/core/src/store/remote/released-reducer-values/index.ts +20 -16
- package/packages/core/src/store/retention.ts +23 -1
- package/packages/core/src/store/rollups.ts +337 -0
- package/packages/core/src/store/session-handle.ts +20 -2
- package/packages/core/src/store/snapshots.ts +10 -0
- package/packages/core/src/store/types.ts +108 -9
- package/packages/core/src/testing/dispatch-journals.ts +117 -0
- package/packages/core/src/testing/dispatch-read-budget.ts +1092 -2
- package/packages/core/src/testing/git-config-isolation.ts +7 -5
- package/packages/core/src/testing/index.ts +27 -0
- package/packages/core/src/testing/memory-operator-sandbox.ts +346 -0
- package/packages/core/src/testing/reduced-read-budget.ts +1873 -98
- package/packages/core/src/web/analytics-source.ts +303 -0
- package/packages/core/src/web/assets/Atkinson-OFL.txt +93 -0
- package/packages/core/src/web/assets/AtkinsonHyperlegibleNext.latin.woff2 +0 -0
- package/packages/core/src/web/assets/JetBrains-OFL.txt +93 -0
- package/packages/core/src/web/assets/JetBrainsMono.latin.woff2 +0 -0
- package/packages/core/src/web/assets/app.css +1 -0
- package/packages/core/src/web/assets/app.js +9 -0
- package/packages/core/src/web/assets/manifest.json +31 -0
- package/packages/core/src/web/assets/theme.js +1 -0
- package/packages/core/src/web/bounded.ts +31 -0
- package/packages/core/src/web/http.ts +315 -0
- package/packages/core/src/web/hub.ts +160 -0
- package/packages/core/src/web/options.ts +38 -0
- package/packages/core/src/web/overview-source.ts +322 -0
- package/packages/core/src/web/page.ts +56 -0
- package/packages/core/src/web/protocol.ts +151 -0
- package/packages/core/src/web/read-only.ts +23 -0
- package/packages/core/src/web/repository-source.ts +39 -0
- package/packages/core/src/web/server.ts +97 -0
- package/skills/code-review/SKILL.md +10 -2
- package/skills/guide/SKILL.md +376 -54
- package/skills/guide/references/accounts.md +2 -1
- package/skills/guide/references/integration-tree.md +2 -2
- package/skills/guide/references/operator-api.md +134 -6
- package/skills/guide/references/optimization-nodes.md +69 -11
- package/skills/guide/references/plugin-authoring.md +37 -1
- package/skills/guide/references/remote-store-protocol.md +94 -13
- package/skills/guide/references/remote-ticket-protocol.md +40 -0
- package/skills/guide/references/restacking.md +20 -4
- package/skills/guide/references/setup.md +14 -2
- package/skills/guide/references/ticket-dependencies.md +10 -3
- package/skills/implement/SKILL.md +23 -4
- package/skills/operate/SKILL.md +134 -32
- package/skills/plan/SKILL.md +10 -4
- package/skills/plan-review/SKILL.md +9 -2
- package/skills/spec/SKILL.md +34 -8
- package/skills/tickets/SKILL.md +51 -5
- package/templates/hosted-service/package.json +2 -2
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
1
2
|
import { validateTicketTree } from './tree'
|
|
2
3
|
import { ticketRevision } from './revision'
|
|
3
4
|
/**
|
|
@@ -18,11 +19,15 @@ import type {
|
|
|
18
19
|
TicketCreateOptions,
|
|
19
20
|
TicketDraft,
|
|
20
21
|
TicketListing,
|
|
22
|
+
TicketSearchHit,
|
|
23
|
+
TicketSearchPage,
|
|
24
|
+
TicketSearchQuery,
|
|
21
25
|
TicketSource,
|
|
22
26
|
TicketUpdate,
|
|
23
27
|
} from '../types'
|
|
24
28
|
import { TicketSourceRateLimitError } from '../types'
|
|
25
29
|
import { validateTicketUpdate } from './update'
|
|
30
|
+
import { TicketSearchCursorInvalid, searchTerms } from './search'
|
|
26
31
|
import { refuseUnsupportedIssueLinks } from './issue-links'
|
|
27
32
|
|
|
28
33
|
export const LINEAR_API_URL = 'https://api.linear.app/graphql'
|
|
@@ -254,6 +259,69 @@ function nextCursor(info: PageInfo | undefined, seen: Set<string>): string | nul
|
|
|
254
259
|
return cursor
|
|
255
260
|
}
|
|
256
261
|
|
|
262
|
+
// Search: one team-filtered `issues` request per page. Linear evaluates the
|
|
263
|
+
// text match itself through IssueFilter comparators, so results follow its
|
|
264
|
+
// own matching and cost never grows with the team's issue count. Checked
|
|
265
|
+
// against Linear's published schema
|
|
266
|
+
// (https://github.com/linear/linear/blob/master/packages/sdk/src/schema.graphql)
|
|
267
|
+
// and documentation:
|
|
268
|
+
// https://linear.app/developers/filtering (containsIgnoreCase, and/or)
|
|
269
|
+
// https://linear.app/developers/pagination (first/after/endCursor)
|
|
270
|
+
// https://linear.app/developers/rate-limiting (complexity).
|
|
271
|
+
// `searchIssues` is rejected: its relevance-by-meaning retrieval and
|
|
272
|
+
// candidate-narrowing filter can silently miss filtered matches.
|
|
273
|
+
// Complexity per hit: 1.2 scalars and state + labels 1 + 50 * 1.1 + 1.2 +
|
|
274
|
+
// nodeLabels 1 + 1.1 + parent 1 + .1 + 1.1 + 2.1 ≈ 65, so at the contract's
|
|
275
|
+
// maximum limit of 100: 100 * 65 + wrappers < 6,600. Bodies are never selected.
|
|
276
|
+
const SEARCH_LABEL_PAGE_SIZE = 50
|
|
277
|
+
const SEARCH_QUERY = `query SearchIssues($filter: IssueFilter!, $first: Int!, $after: String, $nodeLabel: IssueLabelFilter!) { issues(filter: $filter, first: $first, after: $after, includeArchived: true, orderBy: createdAt) { nodes { identifier title state { name } labels(first: ${SEARCH_LABEL_PAGE_SIZE}) { nodes { name } ${PAGE_INFO} } nodeLabels: labels(filter: $nodeLabel, first: 1) { nodes { name } } parent { identifier team { key } labels(filter: $nodeLabel, first: 1) { nodes { name } } } } ${PAGE_INFO} } }`
|
|
278
|
+
const SEARCH_CURSOR_PREFIX = 'linear1.'
|
|
279
|
+
|
|
280
|
+
interface GqlSearchIssue {
|
|
281
|
+
identifier: string
|
|
282
|
+
title: string
|
|
283
|
+
state: { name: string } | null
|
|
284
|
+
labels: Connection<{ name: string }>
|
|
285
|
+
nodeLabels: { nodes: Array<{ name: string }> }
|
|
286
|
+
parent: {
|
|
287
|
+
identifier: string
|
|
288
|
+
team: { key: string } | null
|
|
289
|
+
labels: { nodes: Array<{ name: string }> }
|
|
290
|
+
} | null
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Binds a cursor to the source's team and the normalized query. */
|
|
294
|
+
function searchKey(teamKey: string, terms: string[], state: string | undefined, labels: string[]) {
|
|
295
|
+
return createHash('sha256')
|
|
296
|
+
.update(JSON.stringify([teamKey, terms, state ?? null, labels]))
|
|
297
|
+
.digest('hex')
|
|
298
|
+
.slice(0, 16)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function encodeSearchCursor(key: string, after: string): string {
|
|
302
|
+
return (
|
|
303
|
+
SEARCH_CURSOR_PREFIX +
|
|
304
|
+
Buffer.from(JSON.stringify({ k: key, a: after }), 'utf8').toString('base64url')
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Linear's `endCursor` for this exact search, or null when the cursor is not one. */
|
|
309
|
+
function decodeSearchCursor(cursor: string, key: string): string | null {
|
|
310
|
+
if (!cursor.startsWith(SEARCH_CURSOR_PREFIX)) return null
|
|
311
|
+
const encoded = cursor.slice(SEARCH_CURSOR_PREFIX.length)
|
|
312
|
+
if (!/^[A-Za-z0-9_-]+$/.test(encoded)) return null
|
|
313
|
+
let payload: unknown
|
|
314
|
+
try {
|
|
315
|
+
payload = JSON.parse(Buffer.from(encoded, 'base64url').toString('utf8'))
|
|
316
|
+
} catch {
|
|
317
|
+
return null
|
|
318
|
+
}
|
|
319
|
+
if (payload === null || typeof payload !== 'object') return null
|
|
320
|
+
const { k, a } = payload as { k?: unknown; a?: unknown }
|
|
321
|
+
if (typeof k !== 'string' || typeof a !== 'string' || a.trim() === '' || k !== key) return null
|
|
322
|
+
return a
|
|
323
|
+
}
|
|
324
|
+
|
|
257
325
|
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} }`
|
|
258
326
|
const GET_DETAIL_QUERY = `query GetIssue($id: String!) { issue(id: $id) { ${DETAIL_FIELDS} } }`
|
|
259
327
|
const GET_ISSUE_QUERY = `query GetIssue($id: String!) { issue(id: $id) { ${ISSUE_FIELDS} } }`
|
|
@@ -564,6 +632,68 @@ export class LinearTicketSource implements TicketSource {
|
|
|
564
632
|
return { tickets, diagnostics: [] }
|
|
565
633
|
}
|
|
566
634
|
|
|
635
|
+
/** One team-filtered `issues` request per page: every term must occur
|
|
636
|
+
* (Linear's `containsIgnoreCase`) in the title or the description; state and
|
|
637
|
+
* labels filter exactly as `listReady` does. Newest first, archived issues
|
|
638
|
+
* included. Never a follow-up read per hit and never a listing fallback. */
|
|
639
|
+
async search(query: TicketSearchQuery): Promise<TicketSearchPage> {
|
|
640
|
+
const terms = [...new Set(searchTerms(query.text))]
|
|
641
|
+
const labels = [...new Set(query.labels ?? [])].sort()
|
|
642
|
+
const key = searchKey(this.teamKey, terms, query.state, labels)
|
|
643
|
+
let after: string | null = null
|
|
644
|
+
if (query.cursor !== undefined) {
|
|
645
|
+
after = decodeSearchCursor(query.cursor, key)
|
|
646
|
+
if (after === null) throw new TicketSearchCursorInvalid(this.name, query.cursor)
|
|
647
|
+
}
|
|
648
|
+
const filter: Record<string, unknown> = {
|
|
649
|
+
team: { key: { eq: this.teamKey } },
|
|
650
|
+
...(query.state !== undefined ? { state: { name: { eq: query.state } } } : {}),
|
|
651
|
+
and: [
|
|
652
|
+
...terms.map((term) => ({
|
|
653
|
+
or: [
|
|
654
|
+
{ title: { containsIgnoreCase: term } },
|
|
655
|
+
{ description: { containsIgnoreCase: term } },
|
|
656
|
+
],
|
|
657
|
+
})),
|
|
658
|
+
...labels.map((label) => ({
|
|
659
|
+
labels: { some: { name: { eq: label } } },
|
|
660
|
+
})),
|
|
661
|
+
],
|
|
662
|
+
}
|
|
663
|
+
const data = await this.gql<{ issues: Connection<GqlSearchIssue> }>('search', SEARCH_QUERY, {
|
|
664
|
+
filter,
|
|
665
|
+
first: query.limit,
|
|
666
|
+
after,
|
|
667
|
+
nodeLabel: { name: { eq: 'node' } },
|
|
668
|
+
})
|
|
669
|
+
const diagnostics: string[] = []
|
|
670
|
+
const tickets = data.issues.nodes.map((issue): TicketSearchHit => {
|
|
671
|
+
if (issue.labels.pageInfo?.hasNextPage === true)
|
|
672
|
+
diagnostics.push(
|
|
673
|
+
`linear search: ${issue.identifier} has more than ${SEARCH_LABEL_PAGE_SIZE} labels; its hit lists the first ${SEARCH_LABEL_PAGE_SIZE}`,
|
|
674
|
+
)
|
|
675
|
+
const parent = issue.parent
|
|
676
|
+
const target =
|
|
677
|
+
parent !== null && parent.team?.key === this.teamKey && parent.labels.nodes.length > 0
|
|
678
|
+
? parent.identifier
|
|
679
|
+
: undefined
|
|
680
|
+
return {
|
|
681
|
+
id: issue.identifier,
|
|
682
|
+
title: issue.title,
|
|
683
|
+
...(issue.state?.name !== undefined ? { state: issue.state.name } : {}),
|
|
684
|
+
labels: issue.labels.nodes.map((label) => label.name),
|
|
685
|
+
kind: issue.nodeLabels.nodes.length > 0 ? 'node' : 'work',
|
|
686
|
+
...(target !== undefined ? { target } : {}),
|
|
687
|
+
}
|
|
688
|
+
})
|
|
689
|
+
const next = nextCursor(data.issues.pageInfo, new Set())
|
|
690
|
+
return {
|
|
691
|
+
tickets,
|
|
692
|
+
...(next !== null ? { cursor: encodeSearchCursor(key, next) } : {}),
|
|
693
|
+
diagnostics,
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
567
697
|
async get(id: string): Promise<Ticket | null> {
|
|
568
698
|
return this.readTicket(id, false)
|
|
569
699
|
}
|
|
@@ -49,6 +49,15 @@ export function urgentFirst<T extends Pick<Ticket, 'priority'>>(tickets: readonl
|
|
|
49
49
|
return [...urgent, ...normal]
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/** Stable partition by a caller's tier: urgent first, then the rest, each in
|
|
53
|
+
* input order. */
|
|
54
|
+
export function urgentFirstBy<T>(tickets: readonly T[], tier: (ticket: T) => TicketPriority): T[] {
|
|
55
|
+
const urgent: T[] = []
|
|
56
|
+
const normal: T[] = []
|
|
57
|
+
for (const ticket of tickets) (tier(ticket) === 'urgent' ? urgent : normal).push(ticket)
|
|
58
|
+
return [...urgent, ...normal]
|
|
59
|
+
}
|
|
60
|
+
|
|
52
61
|
export function parsePriorityFlag(value: string, flag = '--priority'): TicketPriority {
|
|
53
62
|
const result = ticketPrioritySchema.safeParse(value.trim())
|
|
54
63
|
if (!result.success) throw new Error(`${flag}: expected urgent or normal, got "${value}"`)
|
|
@@ -5,6 +5,7 @@ import { ticketRefSchema } from '../../ontology'
|
|
|
5
5
|
import { refineUniqueIssues } from './issue-links'
|
|
6
6
|
import { holdsState } from './lifecycle'
|
|
7
7
|
import { ticketPrioritySchema } from './priority'
|
|
8
|
+
import { TICKET_SEARCH_MAX_LIMIT } from './search'
|
|
8
9
|
|
|
9
10
|
/** Explicit opt-in: released clients through 0.12 use strict decoders without revisions.
|
|
10
11
|
* Source checkouts can share their release version, so version alone is insufficient. */
|
|
@@ -74,6 +75,13 @@ export const TICKET_TARGETING_CAPABILITY = '1'
|
|
|
74
75
|
export const TICKET_PAGING_HEADER = 'x-autobuild-ticket-paging'
|
|
75
76
|
export const TICKET_PAGING_CAPABILITY = '1'
|
|
76
77
|
|
|
78
|
+
/** Services that answer `search` echo it on every ticket response, failures
|
|
79
|
+
* included. A client treats an ok response without it, or a 404/400 refusal of
|
|
80
|
+
* `search` without it, as a service predating the operation, and declares
|
|
81
|
+
* search only after a response carried it. */
|
|
82
|
+
export const TICKET_SEARCH_HEADER = 'x-autobuild-ticket-search'
|
|
83
|
+
export const TICKET_SEARCH_CAPABILITY = '1'
|
|
84
|
+
|
|
77
85
|
/** The node id a client's `list-targeting` negotiation probe names. Hosted
|
|
78
86
|
* readable ids are `<teamKey>-<n>` or legacy `ticket-<uuid>`, so it never
|
|
79
87
|
* names a ticket and the probe is an indexed miss. */
|
|
@@ -121,6 +129,23 @@ export const ticketPageWireSchema = z.strictObject({
|
|
|
121
129
|
diagnostics: z.array(z.string()),
|
|
122
130
|
total: z.number().int().nonnegative(),
|
|
123
131
|
})
|
|
132
|
+
/** `search` answers compact hits, never ticket bodies, and the next cursor. */
|
|
133
|
+
export const ticketSearchHitWireSchema = z.strictObject({
|
|
134
|
+
id: z.string().min(1),
|
|
135
|
+
title: z.string(),
|
|
136
|
+
state: z.string().optional(),
|
|
137
|
+
labels: z.array(z.string()),
|
|
138
|
+
kind: z.enum(['work', 'node']),
|
|
139
|
+
target: z
|
|
140
|
+
.string()
|
|
141
|
+
.refine((v) => v.trim().length > 0)
|
|
142
|
+
.optional(),
|
|
143
|
+
})
|
|
144
|
+
export const ticketSearchPageWireSchema = z.strictObject({
|
|
145
|
+
tickets: z.array(ticketSearchHitWireSchema),
|
|
146
|
+
cursor: z.string().min(1).optional(),
|
|
147
|
+
diagnostics: z.array(z.string()),
|
|
148
|
+
})
|
|
124
149
|
export const dependencyStateWireSchema = z.strictObject({
|
|
125
150
|
id: z.string(),
|
|
126
151
|
exists: z.boolean(),
|
|
@@ -206,6 +231,15 @@ export const hostedTicketRequestSchemas = {
|
|
|
206
231
|
}),
|
|
207
232
|
}),
|
|
208
233
|
),
|
|
234
|
+
search: envelope(
|
|
235
|
+
z.strictObject({
|
|
236
|
+
text: z.string().regex(/\S/, 'search text must be nonblank'),
|
|
237
|
+
state: z.string().min(1).optional(),
|
|
238
|
+
labels: z.array(z.string().min(1)).optional(),
|
|
239
|
+
limit: z.number().int().min(1).max(TICKET_SEARCH_MAX_LIMIT),
|
|
240
|
+
cursor: z.string().min(1).optional(),
|
|
241
|
+
}),
|
|
242
|
+
),
|
|
209
243
|
states: envelope(z.strictObject({})),
|
|
210
244
|
'state-counts': envelope(z.strictObject({ names: z.array(z.string().min(1)).min(1).max(16) })),
|
|
211
245
|
'add-state': envelope(z.strictObject({ name: z.string(), about: z.string().optional() })),
|
|
@@ -4,6 +4,7 @@ import { TicketRevisionConflict } from './revision'
|
|
|
4
4
|
import { TicketIssueLinksUnsupported } from './issue-links'
|
|
5
5
|
import { TicketPriorityUnsupported } from './priority'
|
|
6
6
|
import { ticketLifecycle } from './lifecycle'
|
|
7
|
+
import { TicketSearchCursorInvalid, TicketSearchUnsupported } from './search'
|
|
7
8
|
import type { TicketsConfig } from '../../config/schema'
|
|
8
9
|
import type {
|
|
9
10
|
DependencyState,
|
|
@@ -14,6 +15,8 @@ import type {
|
|
|
14
15
|
TicketListing,
|
|
15
16
|
TicketPage,
|
|
16
17
|
TicketPageListing,
|
|
18
|
+
TicketSearchPage,
|
|
19
|
+
TicketSearchQuery,
|
|
17
20
|
TicketSource,
|
|
18
21
|
TicketUpdate,
|
|
19
22
|
TicketMutation,
|
|
@@ -45,11 +48,14 @@ import {
|
|
|
45
48
|
TICKET_TARGETING_PROBE_NODE,
|
|
46
49
|
TICKET_PAGING_HEADER,
|
|
47
50
|
TICKET_PAGING_CAPABILITY,
|
|
51
|
+
TICKET_SEARCH_HEADER,
|
|
52
|
+
TICKET_SEARCH_CAPABILITY,
|
|
48
53
|
dependencyStatesWireSchema,
|
|
49
54
|
type HostedTicketContext,
|
|
50
55
|
successWireSchema,
|
|
51
56
|
ticketListingWireSchema,
|
|
52
57
|
ticketPageWireSchema,
|
|
58
|
+
ticketSearchPageWireSchema,
|
|
53
59
|
ticketWireSchema,
|
|
54
60
|
ticketStatesWireSchema,
|
|
55
61
|
ticketStateCountsWireSchema,
|
|
@@ -112,10 +118,10 @@ export class HostedTicketPagingError extends Error {
|
|
|
112
118
|
}
|
|
113
119
|
}
|
|
114
120
|
|
|
115
|
-
/** Full TicketSource client for a hosted Autobuild service. Targeted listing
|
|
116
|
-
*
|
|
117
|
-
* has carried its echo; product code obtains the source through
|
|
118
|
-
* `createTicketSource`, whose construction-time response settles
|
|
121
|
+
/** Full TicketSource client for a hosted Autobuild service. Targeted listing,
|
|
122
|
+
* native paging and search are each declared only after a response from the
|
|
123
|
+
* service has carried its echo; product code obtains the source through
|
|
124
|
+
* `createTicketSource`, whose construction-time response settles all three. */
|
|
119
125
|
export class HostedTicketSource implements TicketSource {
|
|
120
126
|
readonly name = 'hosted'
|
|
121
127
|
readonly integrationTree = true
|
|
@@ -134,6 +140,9 @@ export class HostedTicketSource implements TicketSource {
|
|
|
134
140
|
private pagingSupport: boolean | undefined
|
|
135
141
|
private readonly paged = (criteria: TicketCriteria, page: TicketPage) =>
|
|
136
142
|
this.pagedListing(criteria, page)
|
|
143
|
+
/** Unknown until a response settles it; see `noteCapabilities`. */
|
|
144
|
+
private searchSupport: boolean | undefined
|
|
145
|
+
private readonly searched = (query: TicketSearchQuery) => this.searchPage(query)
|
|
137
146
|
|
|
138
147
|
constructor(options: HostedTicketSourceOptions) {
|
|
139
148
|
this.requestedIds = hostedRequestedIds(options.teamKey)
|
|
@@ -182,6 +191,13 @@ export class HostedTicketSource implements TicketSource {
|
|
|
182
191
|
return this.pagingSupport === true ? this.paged : undefined
|
|
183
192
|
}
|
|
184
193
|
|
|
194
|
+
/** Present only once the service has positively advertised `search`; while
|
|
195
|
+
* support is unknown or refused, callers refuse with `TicketSearchUnsupported`
|
|
196
|
+
* before any request. There is never a listing fallback. */
|
|
197
|
+
get search(): TicketSource['search'] {
|
|
198
|
+
return this.searchSupport === true ? this.searched : undefined
|
|
199
|
+
}
|
|
200
|
+
|
|
185
201
|
/** Settles targeted-listing support before any observation runs: free when a
|
|
186
202
|
* response already settled it, otherwise one `list-targeting` probe for a
|
|
187
203
|
* node id that can never exist. Throws when the answer is inconclusive. */
|
|
@@ -199,7 +215,8 @@ export class HostedTicketSource implements TicketSource {
|
|
|
199
215
|
}
|
|
200
216
|
|
|
201
217
|
/** Any response carrying an echo declares that capability; an ok response
|
|
202
|
-
* without it comes from a service predating `list-targeting
|
|
218
|
+
* without it comes from a service predating `list-targeting`, `list-page` or
|
|
219
|
+
* `search`.
|
|
203
220
|
* A non-ok response without it (a gateway page, say) proves nothing. */
|
|
204
221
|
private noteCapabilities(response: Response): void {
|
|
205
222
|
if (response.headers.get(TICKET_TARGETING_HEADER) === TICKET_TARGETING_CAPABILITY)
|
|
@@ -208,6 +225,9 @@ export class HostedTicketSource implements TicketSource {
|
|
|
208
225
|
if (response.headers.get(TICKET_PAGING_HEADER) === TICKET_PAGING_CAPABILITY)
|
|
209
226
|
this.pagingSupport = true
|
|
210
227
|
else if (response.ok) this.pagingSupport = false
|
|
228
|
+
if (response.headers.get(TICKET_SEARCH_HEADER) === TICKET_SEARCH_CAPABILITY)
|
|
229
|
+
this.searchSupport = true
|
|
230
|
+
else if (response.ok) this.searchSupport = false
|
|
211
231
|
}
|
|
212
232
|
|
|
213
233
|
private async send(operation: string, input: unknown): Promise<Response> {
|
|
@@ -226,6 +246,7 @@ export class HostedTicketSource implements TicketSource {
|
|
|
226
246
|
[TICKET_POLICY_CREATE_LABELS_HEADER]: TICKET_POLICY_CREATE_LABELS_CAPABILITY,
|
|
227
247
|
[TICKET_TARGETING_HEADER]: TICKET_TARGETING_CAPABILITY,
|
|
228
248
|
[TICKET_PAGING_HEADER]: TICKET_PAGING_CAPABILITY,
|
|
249
|
+
[TICKET_SEARCH_HEADER]: TICKET_SEARCH_CAPABILITY,
|
|
229
250
|
[AUTOBUILD_VERSION_HEADER]: AUTOBUILD_VERSION,
|
|
230
251
|
[REMOTE_STORE_PROTOCOL_VERSION_HEADER]: REMOTE_STORE_PROTOCOL_VERSION,
|
|
231
252
|
},
|
|
@@ -278,6 +299,15 @@ export class HostedTicketSource implements TicketSource {
|
|
|
278
299
|
'ticket service predates paged listing (list-page); upgrade the service',
|
|
279
300
|
)
|
|
280
301
|
}
|
|
302
|
+
// Likewise for search, which never falls back to a listing.
|
|
303
|
+
if (
|
|
304
|
+
operation === 'search' &&
|
|
305
|
+
(response.status === 404 || response.status === 400) &&
|
|
306
|
+
response.headers.get(TICKET_SEARCH_HEADER) !== TICKET_SEARCH_CAPABILITY
|
|
307
|
+
) {
|
|
308
|
+
this.searchSupport = false
|
|
309
|
+
throw new TicketSearchUnsupported(this.name)
|
|
310
|
+
}
|
|
281
311
|
// A service predating createLabels or the roles refuses them with its
|
|
282
312
|
// strict policy schema and never echoes the capability header.
|
|
283
313
|
if (
|
|
@@ -317,6 +347,9 @@ export class HostedTicketSource implements TicketSource {
|
|
|
317
347
|
? body.error
|
|
318
348
|
: 'ticket service cannot page this ticket source',
|
|
319
349
|
)
|
|
350
|
+
if (body.code === 'search-unsupported') throw new TicketSearchUnsupported(this.name)
|
|
351
|
+
if (body.code === 'search-cursor-invalid')
|
|
352
|
+
throw new TicketSearchCursorInvalid(this.name, String(body.cursor ?? ''))
|
|
320
353
|
message = errorBodySchema.parse(body).error
|
|
321
354
|
// A server predating issue links or priority refuses the unknown key with
|
|
322
355
|
// its strict request schema and never echoes the capability header.
|
|
@@ -339,7 +372,9 @@ export class HostedTicketSource implements TicketSource {
|
|
|
339
372
|
error instanceof TicketRevisionConflict ||
|
|
340
373
|
error instanceof TicketIssueLinksUnsupported ||
|
|
341
374
|
error instanceof TicketPriorityUnsupported ||
|
|
342
|
-
error instanceof HostedTicketPagingError
|
|
375
|
+
error instanceof HostedTicketPagingError ||
|
|
376
|
+
error instanceof TicketSearchUnsupported ||
|
|
377
|
+
error instanceof TicketSearchCursorInvalid
|
|
343
378
|
)
|
|
344
379
|
throw error
|
|
345
380
|
// Keep the status-only safe fallback for non-protocol responses.
|
|
@@ -419,6 +454,35 @@ export class HostedTicketSource implements TicketSource {
|
|
|
419
454
|
return { ...listing, tickets: listing.tickets.map((ticket) => this.project(ticket)) }
|
|
420
455
|
}
|
|
421
456
|
|
|
457
|
+
/** One `search` request per page; the service's hits and cursor are canonical. */
|
|
458
|
+
private async searchPage(query: TicketSearchQuery): Promise<TicketSearchPage> {
|
|
459
|
+
const page = await this.request(
|
|
460
|
+
'search',
|
|
461
|
+
{
|
|
462
|
+
text: query.text,
|
|
463
|
+
...(query.state !== undefined ? { state: query.state } : {}),
|
|
464
|
+
...(query.labels !== undefined ? { labels: query.labels } : {}),
|
|
465
|
+
limit: query.limit,
|
|
466
|
+
...(query.cursor !== undefined ? { cursor: query.cursor } : {}),
|
|
467
|
+
},
|
|
468
|
+
{
|
|
469
|
+
parse: (value: unknown) => {
|
|
470
|
+
const parsed = ticketSearchPageWireSchema.safeParse(value)
|
|
471
|
+
if (!parsed.success)
|
|
472
|
+
throw new Error(
|
|
473
|
+
`ticket service answered an invalid search page: ${parsed.error.message}`,
|
|
474
|
+
)
|
|
475
|
+
return parsed.data
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
)
|
|
479
|
+
if (page.tickets.length > query.limit)
|
|
480
|
+
throw new Error(
|
|
481
|
+
`ticket service answered an invalid search page: ${page.tickets.length} hits for limit ${query.limit}`,
|
|
482
|
+
)
|
|
483
|
+
return page
|
|
484
|
+
}
|
|
485
|
+
|
|
422
486
|
async listStates() {
|
|
423
487
|
return this.request('states', {}, ticketStatesWireSchema)
|
|
424
488
|
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Ticket search: the shared matcher and pager for in-memory sources, the
|
|
2
|
+
// typed refusals, and the one caller-side entry point. Dependency-light on
|
|
3
|
+
// purpose — file.ts and fake.ts import it — so it imports only port types.
|
|
4
|
+
import type {
|
|
5
|
+
Ticket,
|
|
6
|
+
TicketSearchHit,
|
|
7
|
+
TicketSearchPage,
|
|
8
|
+
TicketSearchQuery,
|
|
9
|
+
TicketSource,
|
|
10
|
+
} from '../types'
|
|
11
|
+
|
|
12
|
+
export const TICKET_SEARCH_DEFAULT_LIMIT = 20
|
|
13
|
+
export const TICKET_SEARCH_MAX_LIMIT = 100
|
|
14
|
+
|
|
15
|
+
/** The configured source has no `search`; nothing was read. */
|
|
16
|
+
export class TicketSearchUnsupported extends Error {
|
|
17
|
+
constructor(readonly source: string) {
|
|
18
|
+
super(`${source} ticket source does not support search; no tickets were read`)
|
|
19
|
+
this.name = 'TicketSearchUnsupported'
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A cursor this source did not issue for this search. */
|
|
24
|
+
export class TicketSearchCursorInvalid extends Error {
|
|
25
|
+
constructor(
|
|
26
|
+
readonly source: string,
|
|
27
|
+
readonly cursor: string,
|
|
28
|
+
) {
|
|
29
|
+
super(
|
|
30
|
+
`invalid search cursor "${cursor}" for the ${source} ticket source — pass the cursor from the previous page of the same search`,
|
|
31
|
+
)
|
|
32
|
+
this.name = 'TicketSearchCursorInvalid'
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Lowercased whitespace-separated terms; empty terms dropped. */
|
|
37
|
+
export function searchTerms(text: string): string[] {
|
|
38
|
+
return text
|
|
39
|
+
.toLowerCase()
|
|
40
|
+
.split(/\s+/)
|
|
41
|
+
.filter((term) => term !== '')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Every term occurs in the title or the body (each term may hit either). */
|
|
45
|
+
export function matchesSearch(ticket: Pick<Ticket, 'title' | 'body'>, terms: string[]): boolean {
|
|
46
|
+
const title = ticket.title.toLowerCase()
|
|
47
|
+
const body = ticket.body.toLowerCase()
|
|
48
|
+
return terms.every((term) => title.includes(term) || body.includes(term))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function searchHit(ticket: Ticket): TicketSearchHit {
|
|
52
|
+
return {
|
|
53
|
+
id: ticket.ref.id,
|
|
54
|
+
title: ticket.title,
|
|
55
|
+
...(ticket.state !== undefined ? { state: ticket.state } : {}),
|
|
56
|
+
labels: [...ticket.labels],
|
|
57
|
+
kind: ticket.kind ?? 'work',
|
|
58
|
+
...(ticket.target !== undefined ? { target: ticket.target } : {}),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Search an in-memory listing in its own order. `query.state` must already be
|
|
63
|
+
* the source's canonical state name. The cursor is a decimal match offset. */
|
|
64
|
+
export function pageSearch(
|
|
65
|
+
sourceName: string,
|
|
66
|
+
tickets: Ticket[],
|
|
67
|
+
query: TicketSearchQuery,
|
|
68
|
+
diagnostics: string[] = [],
|
|
69
|
+
): TicketSearchPage {
|
|
70
|
+
const terms = searchTerms(query.text)
|
|
71
|
+
const labels = query.labels ?? []
|
|
72
|
+
const matches = tickets.filter(
|
|
73
|
+
(ticket) =>
|
|
74
|
+
(query.state === undefined || ticket.state === query.state) &&
|
|
75
|
+
labels.every((label) => ticket.labels.includes(label)) &&
|
|
76
|
+
matchesSearch(ticket, terms),
|
|
77
|
+
)
|
|
78
|
+
const offset = decodeOffset(sourceName, query.cursor)
|
|
79
|
+
const end = offset + query.limit
|
|
80
|
+
return {
|
|
81
|
+
tickets: matches.slice(offset, end).map(searchHit),
|
|
82
|
+
...(end < matches.length ? { cursor: String(end) } : {}),
|
|
83
|
+
diagnostics: [...diagnostics],
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function decodeOffset(sourceName: string, cursor: string | undefined): number {
|
|
88
|
+
if (cursor === undefined) return 0
|
|
89
|
+
if (!/^(0|[1-9]\d*)$/.test(cursor)) throw new TicketSearchCursorInvalid(sourceName, cursor)
|
|
90
|
+
const offset = Number(cursor)
|
|
91
|
+
if (!Number.isSafeInteger(offset)) throw new TicketSearchCursorInvalid(sourceName, cursor)
|
|
92
|
+
return offset
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface TicketSearchInput {
|
|
96
|
+
text: string
|
|
97
|
+
state?: string
|
|
98
|
+
labels?: string[]
|
|
99
|
+
limit?: number
|
|
100
|
+
cursor?: string
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The one caller-side entry point (operator tool and CLI). Refuses a source
|
|
104
|
+
* without `search` before calling any source method — never a listing
|
|
105
|
+
* fallback — and returns a JSON-clean, normalized page. */
|
|
106
|
+
export async function searchTickets(
|
|
107
|
+
source: TicketSource,
|
|
108
|
+
input: TicketSearchInput,
|
|
109
|
+
): Promise<TicketSearchPage> {
|
|
110
|
+
if (input.text.trim() === '') throw new Error('ticket search needs a nonblank query')
|
|
111
|
+
const limit = input.limit ?? TICKET_SEARCH_DEFAULT_LIMIT
|
|
112
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > TICKET_SEARCH_MAX_LIMIT) {
|
|
113
|
+
throw new Error(`ticket search limit must be an integer from 1 to ${TICKET_SEARCH_MAX_LIMIT}`)
|
|
114
|
+
}
|
|
115
|
+
if (typeof source.search !== 'function') throw new TicketSearchUnsupported(source.name)
|
|
116
|
+
const page = await source.search({
|
|
117
|
+
text: input.text,
|
|
118
|
+
...(input.state !== undefined ? { state: input.state } : {}),
|
|
119
|
+
...(input.labels !== undefined ? { labels: [...input.labels] } : {}),
|
|
120
|
+
limit,
|
|
121
|
+
...(input.cursor !== undefined ? { cursor: input.cursor } : {}),
|
|
122
|
+
})
|
|
123
|
+
return {
|
|
124
|
+
tickets: page.tickets.map(normalizeHit),
|
|
125
|
+
...(page.cursor !== undefined ? { cursor: page.cursor } : {}),
|
|
126
|
+
diagnostics: [...page.diagnostics],
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function normalizeHit(hit: TicketSearchHit): TicketSearchHit {
|
|
131
|
+
return {
|
|
132
|
+
id: hit.id,
|
|
133
|
+
title: hit.title,
|
|
134
|
+
...(hit.state !== undefined ? { state: hit.state } : {}),
|
|
135
|
+
labels: [...hit.labels],
|
|
136
|
+
kind: hit.kind ?? 'work',
|
|
137
|
+
...(hit.target !== undefined ? { target: hit.target } : {}),
|
|
138
|
+
}
|
|
139
|
+
}
|