-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcreate-task-core.ts
More file actions
264 lines (239 loc) · 11.3 KB
/
create-task-core.ts
File metadata and controls
264 lines (239 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* MIT No Attribution
*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// HTTP create-task path: validation, persistence, orchestrator invoke. Related: orchestrator.ts, preflight.ts.
// Tests: cdk/test/handlers/shared/create-task-core.test.ts, cdk/test/handlers/create-task.test.ts
import { BedrockRuntimeClient, ApplyGuardrailCommand } from '@aws-sdk/client-bedrock-runtime';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda';
import { DynamoDBDocumentClient, PutCommand, QueryCommand, GetCommand } from '@aws-sdk/lib-dynamodb';
import type { APIGatewayProxyResult } from 'aws-lambda';
import { ulid } from 'ulid';
import { generateBranchName } from './gateway';
import { logger } from './logger';
import { checkRepoOnboarded } from './repo-config';
import { ErrorCode, errorResponse, successResponse } from './response';
import { type CreateTaskRequest, isPrTaskType, type TaskRecord, type TaskType, toTaskDetail } from './types';
import { computeTtlEpoch, DEFAULT_MAX_TURNS, hasTaskSpec, isValidIdempotencyKey, isValidRepo, isValidTaskDescriptionLength, isValidTaskType, MAX_TASK_DESCRIPTION_LENGTH, validateMaxBudgetUsd, validateMaxTurns, validatePrNumber } from './validation';
import { TaskStatus } from '../../constructs/task-status';
/**
* Context for task creation — abstracts the auth source (Cognito vs. webhook).
*/
export interface TaskCreationContext {
readonly userId: string;
readonly channelSource: 'api' | 'webhook';
readonly channelMetadata: Record<string, string>;
readonly idempotencyKey?: string;
}
const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const lambdaClient = process.env.ORCHESTRATOR_FUNCTION_ARN ? new LambdaClient({}) : undefined;
const bedrockClient = (process.env.GUARDRAIL_ID && process.env.GUARDRAIL_VERSION)
? new BedrockRuntimeClient({}) : undefined;
if (process.env.GUARDRAIL_ID && !process.env.GUARDRAIL_VERSION) {
logger.error('GUARDRAIL_ID is set but GUARDRAIL_VERSION is missing — guardrail screening disabled', {
metric_type: 'guardrail_misconfiguration',
});
}
const TABLE_NAME = process.env.TASK_TABLE_NAME!;
const EVENTS_TABLE_NAME = process.env.TASK_EVENTS_TABLE_NAME!;
const TASK_RETENTION_DAYS = Number(process.env.TASK_RETENTION_DAYS ?? '90');
/**
* Core task creation logic shared by the Cognito create-task handler
* and the webhook create-task handler.
* @param body - parsed and type-checked request body.
* @param context - auth context (user, channel, idempotency).
* @param requestId - unique request ID for tracing.
* @returns the API Gateway proxy result.
*/
export async function createTaskCore(
body: CreateTaskRequest,
context: TaskCreationContext,
requestId: string,
): Promise<APIGatewayProxyResult> {
// 1. Validate request body
if (!body.repo || !isValidRepo(body.repo)) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'Invalid or missing repo. Expected format: owner/repo.', requestId);
}
// 1b. Check repo is onboarded (conditional — skipped when REPO_TABLE_NAME is not set)
const onboardingResult = await checkRepoOnboarded(body.repo);
if (!onboardingResult.onboarded) {
return errorResponse(422, ErrorCode.REPO_NOT_ONBOARDED, `Repository '${body.repo}' is not onboarded. Register it with a Blueprint before submitting tasks.`, requestId);
}
if (!hasTaskSpec(body)) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'At least one of issue_number or task_description is required.', requestId);
}
// Validate task_type
if (!isValidTaskType(body.task_type)) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'Invalid task_type. Must be "new_task", "pr_iteration", or "pr_review".', requestId);
}
const taskType: TaskType = (body.task_type as TaskType) ?? 'new_task';
const isPrTask = isPrTaskType(taskType);
// Validate pr_number
const prNumberResult = validatePrNumber(body.pr_number);
if (prNumberResult === null) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'Invalid pr_number. Must be a positive integer.', requestId);
}
if (isPrTask && prNumberResult === undefined) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, `pr_number is required when task_type is "${taskType}".`, requestId);
}
if (!isPrTask && prNumberResult !== undefined) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'pr_number is only allowed when task_type is "pr_iteration" or "pr_review".', requestId);
}
if (body.task_description && !isValidTaskDescriptionLength(body.task_description)) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, `task_description exceeds maximum length of ${MAX_TASK_DESCRIPTION_LENGTH} characters.`, requestId);
}
const maxTurnsResult = validateMaxTurns(body.max_turns);
if (maxTurnsResult === null) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'Invalid max_turns. Must be an integer between 1 and 500.', requestId);
}
// Store only user-explicit max_turns on the task record (undefined when not specified).
// The effective value is computed at orchestration time using the 3-tier override:
// platform default < per-repo Blueprint config < per-task user override.
const userMaxTurns = maxTurnsResult;
const maxBudgetResult = validateMaxBudgetUsd(body.max_budget_usd);
if (maxBudgetResult === null) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'Invalid max_budget_usd. Must be a number between 0.01 and 100.', requestId);
}
const userMaxBudgetUsd = maxBudgetResult;
// 2. Screen task description with Bedrock Guardrail (fail-closed: unscreened content
// must not reach the agent — a Bedrock outage blocks task submissions)
if (bedrockClient && body.task_description) {
try {
const guardrailResult = await bedrockClient.send(new ApplyGuardrailCommand({
guardrailIdentifier: process.env.GUARDRAIL_ID!,
guardrailVersion: process.env.GUARDRAIL_VERSION!,
source: 'INPUT',
content: [{ text: { text: body.task_description } }],
}));
if (guardrailResult.action === 'GUARDRAIL_INTERVENED') {
logger.warn('Task description blocked by guardrail', { user_id: context.userId, request_id: requestId });
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'Task description was blocked by content policy.', requestId);
}
} catch (guardrailErr) {
logger.error('Guardrail screening failed (fail-closed)', {
error: String(guardrailErr),
user_id: context.userId,
request_id: requestId,
metric_type: 'guardrail_screening_failure',
});
return errorResponse(503, ErrorCode.INTERNAL_ERROR, 'Content screening is temporarily unavailable. Please try again later.', requestId);
}
}
// 3. Check idempotency key
if (context.idempotencyKey !== undefined && context.idempotencyKey !== null) {
if (!isValidIdempotencyKey(context.idempotencyKey)) {
return errorResponse(400, ErrorCode.VALIDATION_ERROR, 'Invalid Idempotency-Key format.', requestId);
}
const existing = await ddb.send(new QueryCommand({
TableName: TABLE_NAME,
IndexName: 'IdempotencyIndex',
KeyConditionExpression: 'idempotency_key = :key',
ExpressionAttributeValues: { ':key': context.idempotencyKey },
Limit: 1,
}));
if (existing.Items && existing.Items.length > 0) {
const existingTaskId = existing.Items[0].task_id as string;
const existingTask = await ddb.send(new GetCommand({
TableName: TABLE_NAME,
Key: { task_id: existingTaskId },
}));
if (existingTask.Item) {
return errorResponse(409, ErrorCode.DUPLICATE_TASK, 'A task with this idempotency key already exists.', requestId);
}
}
}
// 4. Generate identifiers and timestamps
const taskId = ulid();
const now = new Date().toISOString();
const branchName = isPrTask
? 'pending:pr_resolution'
: generateBranchName(taskId, body.task_description ?? body.repo);
// 5. Build task record
const taskRecord: TaskRecord = {
task_id: taskId,
user_id: context.userId,
status: TaskStatus.SUBMITTED,
repo: body.repo,
...(body.issue_number !== undefined && { issue_number: body.issue_number }),
task_type: taskType,
...(prNumberResult !== undefined && { pr_number: prNumberResult }),
...(body.task_description !== undefined && { task_description: body.task_description }),
branch_name: branchName,
...(userMaxTurns !== undefined && { max_turns: userMaxTurns }),
...(userMaxBudgetUsd !== undefined && { max_budget_usd: userMaxBudgetUsd }),
...(context.idempotencyKey && { idempotency_key: context.idempotencyKey }),
channel_source: context.channelSource,
channel_metadata: context.channelMetadata,
status_created_at: `${TaskStatus.SUBMITTED}#${now}`,
created_at: now,
updated_at: now,
};
// 6. Write task record
await ddb.send(new PutCommand({
TableName: TABLE_NAME,
Item: taskRecord,
ConditionExpression: 'attribute_not_exists(task_id)',
}));
// 7. Write task_created event (best-effort — event loss is acceptable,
// task record is the source of truth)
try {
await ddb.send(new PutCommand({
TableName: EVENTS_TABLE_NAME,
Item: {
task_id: taskId,
event_id: ulid(),
event_type: 'task_created',
timestamp: now,
ttl: computeTtlEpoch(TASK_RETENTION_DAYS),
metadata: {
repo: body.repo,
issue_number: body.issue_number ?? null,
channel_source: context.channelSource,
},
},
}));
} catch (eventErr) {
logger.error('Failed to write task_created event — task was created successfully', {
task_id: taskId,
error: String(eventErr),
request_id: requestId,
});
}
logger.info('Task created', {
task_id: taskId,
user_id: context.userId,
repo: body.repo,
channel_source: context.channelSource,
request_id: requestId,
});
// 8. Async-invoke the orchestrator (fire-and-forget)
if (lambdaClient && process.env.ORCHESTRATOR_FUNCTION_ARN) {
try {
await lambdaClient.send(new InvokeCommand({
FunctionName: process.env.ORCHESTRATOR_FUNCTION_ARN,
InvocationType: 'Event',
Payload: new TextEncoder().encode(JSON.stringify({ task_id: taskId })),
}));
logger.info('Orchestrator invoked', { task_id: taskId });
} catch (orchErr) {
logger.error('Failed to invoke orchestrator', { error: String(orchErr), task_id: taskId });
}
}
// 9. Return created task
return successResponse(201, toTaskDetail(taskRecord), requestId);
}