Files
archived-orca/src/main/runtime/runtime-linear-task-fields-commands.ts
T
Brennan Benson e69baa3499 fix(linear): stop writes when their caller lifetime ends (#35020)
* fix(linear): bound write preparation and mutation to one caller lifetime

* fix(linear): isolate cancellable preparation from shared reads

* test(linear): keep CLI lifetime coverage in its compiler project

* fix(linear): preserve comment change failure guidance

* test(linear): place CLI and runtime coverage with integration tests

* test(linear): reset CLI exit status across runtimes

* fix(linear): preserve truthful confirmation deadline causes

* test(linear): support renamed workspace selector in fixture

* test(linear): adapt relation lifetime fixture to client factory
2026-10-29 13:15:07 -07:00

283 lines
9.1 KiB
TypeScript

import {
getLinearTeamMembersOrThrow,
isLinearUuid,
getLinearIssueByUuidForAgent,
getLinearTeamStatesOrThrow,
labelsForIds
} from './runtime-linear-command-dependencies'
import { linearError } from '../linear/issue-context/errors'
import { readLinearIssueContext } from '../linear/issue-context/read-context'
import type {
LinearIssueTaskUpdateRequest,
LinearSaveIssueRequest,
LinearIssueUpdate,
LinearCurrentIssueContextHints,
LinearAgentWriteTarget
} from './runtime-linear-command-dependencies'
import { RuntimeLinearLabelWriteCommands } from './runtime-linear-label-write-commands'
export class RuntimeLinearTaskFieldCommands extends RuntimeLinearLabelWriteCommands {
public async getLinearTeamStatesForWrite(
teamId: string,
workspaceId: string,
signal?: AbortSignal
): Promise<Awaited<ReturnType<typeof getLinearTeamStatesOrThrow>>> {
signal?.throwIfAborted()
try {
return await getLinearTeamStatesOrThrow(teamId, workspaceId, signal)
} catch (error) {
throw this.mapLinearReadFailure(error)
}
}
public resolveLinearAgentState(
input: string,
states: Awaited<ReturnType<typeof getLinearTeamStatesOrThrow>>
): Awaited<ReturnType<typeof getLinearTeamStatesOrThrow>>[number] | null {
const normalized = input.toLowerCase()
const exact = states.find(
(state) => state.id.toLowerCase() === normalized || state.name.toLowerCase() === normalized
)
// Accept MCP lifecycle types, but explicit ids/names win collisions.
return exact ?? states.find((state) => state.type.toLowerCase() === normalized) ?? null
}
public async readLinearAgentIssueWriteRecord(
issueId: string,
workspaceId: string,
signal?: AbortSignal
): Promise<NonNullable<Awaited<ReturnType<typeof getLinearIssueByUuidForAgent>>>> {
signal?.throwIfAborted()
const issue = await this.readLinearWriteLookup(() =>
getLinearIssueByUuidForAgent(issueId, workspaceId, signal)
)
if (!issue) {
throw linearError('linear_issue_not_found', 'Linear issue was not found.')
}
return issue
}
public async buildLinearTaskUpdate(
params: LinearIssueTaskUpdateRequest,
current: NonNullable<Awaited<ReturnType<typeof getLinearIssueByUuidForAgent>>>,
workspaceId: string,
signal?: AbortSignal
): Promise<{
fields: {
assigneeId?: string | null
priority?: number
estimate?: number | null
dueDate?: string | null
labelIds?: string[]
}
labels?: { id: string; name: string }[]
} | null> {
signal?.throwIfAborted()
if (params.operation === 'assignee') {
const assigneeId = params.assigneeMe
? (await this.getLinearViewerForWrite(workspaceId, signal)).id
: params.assigneeId
if (assigneeId === undefined) {
throw linearError('linear_invalid_assignee', 'Pass --me, --to-id, or clear assignee.')
}
return { fields: { assigneeId } }
}
if (params.operation === 'priority') {
if (params.priority === undefined) {
throw linearError('linear_write_failed', 'Missing priority value.')
}
return { fields: { priority: params.priority } }
}
if (params.operation === 'estimate') {
if (params.estimate === undefined) {
throw linearError('linear_write_failed', 'Missing estimate value.')
}
return { fields: { estimate: params.estimate } }
}
if (params.operation === 'dueDate') {
if (params.dueDate === undefined) {
throw linearError('linear_write_failed', 'Missing due date value.')
}
return { fields: { dueDate: params.dueDate } }
}
if (params.operation === 'labels') {
const mode = params.labelMode
const inputs = params.labels ?? []
if (!mode || inputs.length === 0) {
throw linearError('linear_invalid_label', 'Pass at least one --label.')
}
const labels = await this.resolveLinearLabelsForIssue(current, inputs, workspaceId, signal)
const requestedIds = labels.map((label) => label.id)
const existingIds = current.labelIds ?? current.labels?.map((label) => label.id) ?? []
const nextIds =
mode === 'set'
? requestedIds
: mode === 'add'
? Array.from(new Set([...existingIds, ...requestedIds]))
: existingIds.filter((id) => !requestedIds.includes(id))
return {
fields: { labelIds: nextIds },
labels: labelsForIds(nextIds, [...(current.labels ?? []), ...labels])
}
}
return null
}
public async buildLinearSaveUpdate(
params: LinearSaveIssueRequest,
current: NonNullable<Awaited<ReturnType<typeof getLinearIssueByUuidForAgent>>>,
workspaceId: string,
signal?: AbortSignal
): Promise<LinearIssueUpdate> {
signal?.throwIfAborted()
const fields: LinearIssueUpdate = {}
if (params.title !== undefined) {
fields.title = params.title
}
if (params.description !== undefined) {
fields.description = params.description
}
if (params.priority !== undefined) {
fields.priority = params.priority
}
if (params.estimate !== undefined) {
fields.estimate = params.estimate
}
if (params.dueDate !== undefined) {
fields.dueDate = params.dueDate
}
if (params.state !== undefined) {
const states = await this.getLinearTeamStatesForWrite(current.team.id, workspaceId, signal)
const state = this.resolveLinearAgentState(params.state, states)
if (!state) {
throw linearError(
'linear_invalid_state',
`No workflow state exactly matched "${params.state}".`
)
}
fields.stateId = state.id
}
if (params.assignee !== undefined) {
fields.assigneeId =
params.assignee === null
? null
: await this.resolveLinearAssignee(params.assignee, current.team.id, workspaceId, signal)
}
if (params.labels !== undefined) {
if (params.labels.length === 0) {
fields.labelIds = []
} else {
const labels = await this.resolveLinearLabelsForIssue(
current,
params.labels,
workspaceId,
signal
)
fields.labelIds = labels.map((label) => label.id)
}
}
if (params.project !== undefined) {
fields.projectId =
params.project === null
? null
: (
await this.resolveLinearCreateProject(
params.project,
{
id: current.team.id,
workspaceId
},
signal
)
).id
}
if (params.parentId !== undefined) {
fields.parentId =
params.parentId === null
? null
: (
await this.resolveLinearAgentWriteTarget(
{
input: params.parentId,
workspaceId,
context: params.context
},
signal
)
).issue.id
if (fields.parentId === current.id) {
throw linearError('linear_invalid_parent', 'An issue cannot be its own parent.')
}
}
return fields
}
public async resolveLinearAssignee(
input: string,
teamId: string,
workspaceId: string,
signal?: AbortSignal
): Promise<string> {
signal?.throwIfAborted()
if (input.toLowerCase() === 'me') {
return (await this.getLinearViewerForWrite(workspaceId, signal)).id
}
// Keep the existing direct-id fast path; avoid a paginated member scan.
if (isLinearUuid(input)) {
return input
}
let members: Awaited<ReturnType<typeof getLinearTeamMembersOrThrow>>
try {
members = await getLinearTeamMembersOrThrow(teamId, workspaceId, signal)
} catch (error) {
throw this.mapLinearReadFailure(error)
}
const normalized = input.toLowerCase()
const matches = members.filter(
(member) =>
member.id.toLowerCase() === normalized ||
member.displayName.toLowerCase() === normalized ||
member.name?.toLowerCase() === normalized ||
member.email?.toLowerCase() === normalized
)
if (matches.length === 1) {
return matches[0].id
}
throw linearError(
'linear_invalid_assignee',
matches.length === 0
? `No team member exactly matched "${input}".`
: `Multiple team members exactly matched "${input}".`
)
}
public async resolveLinearAgentWriteTarget(
params: {
input?: string
current?: boolean
workspaceId?: string
context?: LinearCurrentIssueContextHints
},
signal?: AbortSignal
): Promise<LinearAgentWriteTarget> {
signal?.throwIfAborted()
const result = await readLinearIssueContext(
{
input: params.input,
current: params.current,
workspaceId: params.workspaceId,
include: {
comments: false,
children: false,
attachments: false,
relations: false,
activity: false
},
depth: 0,
context: params.context
},
(context) => this.linearResolveCurrentIssue(context, signal),
signal
)
return { issue: result.issue, workspaceId: result.meta.resolved.workspaceId }
}
}