Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion helm/environments/local/lifecycle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ components:
initialDelaySeconds: 60
periodSeconds: 10
failureThreshold: 6
timeoutSeconds: 10
readinessProbe:
httpGet:
path: /api/health
port: 80
periodSeconds: 5
periodSeconds: 15
failureThreshold: 3
timeoutSeconds: 10

worker:
enabled: true
Expand Down
84 changes: 65 additions & 19 deletions src/app/api/v2/ai/chat/[buildUuid]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import AIAgentConfigService from 'server/services/aiAgentConfig';
import { getLogger, withLogContext } from 'server/lib/logger';
import { extractJsonFromResponse } from 'server/services/ai/utils/jsonExtraction';
import { sanitizeForJson } from 'server/services/ai/utils/sanitize';
import { normalizeInvestigationPayload } from 'server/services/ai/utils/normalizePayload';
import { authorizeToolForFixTarget, FixTargetScope } from 'server/services/ai/utils/fixTargetAuthorization';
import {
createClassifiedError,
ErrorCategory,
Expand Down Expand Up @@ -150,6 +152,25 @@ export const dynamic = 'force-dynamic';
* Operation mode. "investigate" (default) is read-only analysis.
* "fix" enables the agent to take corrective actions via tools.
* default: investigate
* fixTarget:
* type: object
* description: >
* Optional service-scoped fix target. When provided in fix mode,
* mutating tool calls are constrained to this selected issue.
* properties:
* serviceName:
* type: string
* suggestedFix:
* type: string
* filePath:
* type: string
* files:
* type: array
* items:
* type: object
* properties:
* path:
* type: string
* responses:
* '200':
* description: >
Expand Down Expand Up @@ -278,7 +299,7 @@ const postHandler = async (req: NextRequest, { params }: { params: { buildUuid:
return;
}

const { message, clearHistory, provider, modelId, isSystemAction, mode: requestedMode } = body;
const { message, clearHistory, provider, modelId, isSystemAction, mode: requestedMode, fixTarget } = body;

getLogger().info(
`AI: v2 chat request received provider=${provider} modelId=${modelId} hasProvider=${!!provider} hasModelId=${!!modelId}`
Expand Down Expand Up @@ -364,6 +385,13 @@ const postHandler = async (req: NextRequest, { params }: { params: { buildUuid:
getLogger().info(`AI: using mode=${mode} (requestedMode=${requestedMode})`);

const onToolConfirmation = mode === 'fix' ? async () => true : undefined;
const onToolAuthorization =
mode === 'fix' && fixTarget
? async (
tool: { name: string; description: string; category: string; safetyLevel: string },
args: Record<string, unknown>
) => authorizeToolForFixTarget(fixTarget as FixTargetScope, { ...tool, args })
: undefined;

const result = await llmService.processQueryStream(
message,
Expand Down Expand Up @@ -414,6 +442,7 @@ const postHandler = async (req: NextRequest, { params }: { params: { buildUuid:
collectedEvidence.push(evidenceEvent as unknown as Record<string, unknown>);
},
onToolConfirmation,
onToolAuthorization,
mode,
(event: AIChatSSEEvent) => {
try {
Expand Down Expand Up @@ -470,35 +499,46 @@ const postHandler = async (req: NextRequest, { params }: { params: { buildUuid:
aiResponse = result.response;
isJsonResponse = result.isJson;
totalInvestigationTimeMs = result.totalInvestigationTimeMs;
let preambleText: string | undefined = result.preamble;
let completeJsonEmitted = false;

if (!isJsonResponse && aiResponse.includes('"investigation_complete"')) {
if (aiResponse.includes('"investigation_complete"')) {
const extracted = extractJsonFromResponse(aiResponse, buildUuid);
aiResponse = extracted.response;
isJsonResponse = extracted.isJson;
if (extracted.isJson) {
aiResponse = extracted.response;
isJsonResponse = true;
if (extracted.preamble && !preambleText) {
preambleText = extracted.preamble;
}
}
}

if (isJsonResponse) {
try {
const parsed = JSON.parse(aiResponse);
let parsed = JSON.parse(aiResponse);
getLogger().info(
`AI: JSON response type=${parsed.type} hasPullRequest=${!!context.lifecycleContext
?.pullRequest} fullName=${context.lifecycleContext?.pullRequest?.fullName} branch=${
context.lifecycleContext?.pullRequest?.branch
}`
);
if (parsed.type === 'investigation_complete' && context.lifecycleContext?.pullRequest) {
const fullName = context.lifecycleContext.pullRequest.fullName;
const branch = context.lifecycleContext.pullRequest.branch;
if (fullName && branch) {
const [owner, name] = fullName.split('/');
parsed.repository = { owner, name, branch };
getLogger().info(`AI: added repository to response owner=${owner} name=${name} branch=${branch}`);

const sanitized = sanitizeForJson(parsed);
aiResponse = JSON.stringify(sanitized, null, 2);

JSON.parse(aiResponse);
if (parsed.type === 'investigation_complete') {
parsed = normalizeInvestigationPayload(parsed, { availableTools: result.availableTools });

if (context.lifecycleContext?.pullRequest) {
const fullName = context.lifecycleContext.pullRequest.fullName;
const branch = context.lifecycleContext.pullRequest.branch;
if (fullName && branch) {
const [owner, name] = fullName.split('/');
parsed.repository = { owner, name, branch };
getLogger().info(`AI: added repository to response owner=${owner} name=${name} branch=${branch}`);
}
}

const sanitized = sanitizeForJson(parsed);
aiResponse = JSON.stringify(sanitized, null, 2);

JSON.parse(aiResponse);
}
} catch (e) {
getLogger().error(
Expand All @@ -510,8 +550,14 @@ const postHandler = async (req: NextRequest, { params }: { params: { buildUuid:
isJsonResponse = false;
}

if (isJsonResponse) {
sendEvent({ type: 'complete_json', content: aiResponse, totalInvestigationTimeMs });
if (isJsonResponse && !completeJsonEmitted) {
completeJsonEmitted = true;
sendEvent({
type: 'complete_json',
content: aiResponse,
totalInvestigationTimeMs,
...(preambleText ? { preamble: preambleText } : {}),
});
}
}
} catch (error: any) {
Expand Down
18 changes: 17 additions & 1 deletion src/components/ai-agent/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,24 @@ export function useChat({ buildUuid, selectedModel }: UseChatOptions) {
receivedCompleteJson = true;
const debugToolData =
debugToolDataMapRef.current.size > 0 ? Array.from(debugToolDataMapRef.current.values()) : undefined;
const preamble =
typeof data.preamble === 'string' && data.preamble.trim().length > 0 ? data.preamble.trim() : null;
const timestamp = Date.now();
setMessages((prev) => [
...prev,
...(preamble
? [
{
role: 'assistant' as const,
content: preamble,
timestamp,
},
]
: []),
{
role: 'assistant',
content: data.content,
timestamp: Date.now(),
timestamp: timestamp + (preamble ? 1 : 0),
activityHistory: collectedActivities.length > 0 ? collectedActivities : undefined,
evidenceItems: collectedEvidence.length > 0 ? collectedEvidence : undefined,
totalInvestigationTimeMs: data.totalInvestigationTimeMs,
Expand Down Expand Up @@ -420,6 +432,10 @@ export function useChat({ buildUuid, selectedModel }: UseChatOptions) {
};

const handleAutoFix = (service: ServiceInvestigationResult) => {
if (!service.canAutoFix) {
return;
}

let fixMessage = `User consents to fix ${service.serviceName}. `;

if (service.suggestedFix) {
Expand Down
2 changes: 2 additions & 0 deletions src/server/lib/validation/aiAgentConfigSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const aiAgentConfigSchema = {
systemPromptOverride: { type: 'string', maxLength: 50000 },
excludedTools: { type: 'array', items: { type: 'string' } },
excludedFilePatterns: { type: 'array', items: { type: 'string' } },
allowedWritePatterns: { type: 'array', items: { type: 'string' } },
maxIterations: { type: 'integer', minimum: 1 },
maxToolCalls: { type: 'integer', minimum: 1 },
maxRepeatedCalls: { type: 'integer', minimum: 1 },
Expand All @@ -76,6 +77,7 @@ export const aiAgentRepoOverrideSchema = {
systemPromptOverride: { type: 'string', maxLength: 50000 },
excludedTools: { type: 'array', items: { type: 'string' } },
excludedFilePatterns: { type: 'array', items: { type: 'string' } },
allowedWritePatterns: { type: 'array', items: { type: 'string' } },
},
additionalProperties: false,
};
22 changes: 22 additions & 0 deletions src/server/services/ai/orchestration/safety.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ export class ToolSafetyManager {
};
}

if (callbacks.onToolAuthorization) {
const decision = await callbacks.onToolAuthorization(
{
name: tool.name,
description: tool.description,
category: tool.category,
safetyLevel: tool.safetyLevel,
},
args
);
if (!decision.allowed) {
return {
success: false,
error: {
message: decision.reason || `Tool ${tool.name} is not authorized for this operation`,
code: 'TOOL_NOT_AUTHORIZED',
recoverable: false,
},
};
}
}

if (this.needsConfirmation(tool)) {
const confirmDetails = await tool.shouldConfirmExecution?.(args);

Expand Down
15 changes: 9 additions & 6 deletions src/server/services/ai/prompts/sections/investigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ Vague messages ("something seems wrong", "help") — ask for clarification befor
1. Call get_file — fetch current content. This is your ground truth.
2. Copy the file content EXACTLY. Change ONLY the lines needed for the fix. Do not remove comments, delete unused sections, reformat, or make any other modifications.
3. Before calling update_file, verify: does your new_content differ from the original in ONLY the intended lines? If you changed anything else, redo step 2.
4. Call update_file or commit_lifecycle_fix with the verified content.
4. Execute ONLY the mutating tool needed for the selected issue:
- File content fixes: update_file
- PR label fixes: update_pr_labels
- Runtime K8s patch fixes: patch_k8s_resource
5. Verify success response, extract commit_url.
6. Output JSON with fixesApplied: true + commitUrl.

Expand All @@ -65,7 +68,7 @@ Decide your output format using this decision tree:
- Any issue found OR config problem even if pods running → JSON schema below
- Fix applied successfully → JSON with fixesApplied: true and commitUrl

JSON — output ONLY valid JSON (no markdown, no conversational text):
JSON — output ONLY the raw JSON object. Do not include any markdown preamble, conversational text, or code fences before or after it. If you want to provide a conversational summary, put it in the "summary" field of the JSON. The response must start with \`{\` and end with \`}\`:

<output_schema>
{
Expand Down Expand Up @@ -111,9 +114,9 @@ JSON — output ONLY valid JSON (no markdown, no conversational text):

**status:** Use lowercase values: \`build_failed\`, \`deploy_failed\`, \`error\`, \`ready\`. These must match the TypeScript union type exactly.

**canAutoFix=true** ONLY when: (1) a specific error message from logs/K8s/build output points to the problem, (2) file read + verified the fix, (3) 100% certain. False when: no error messages found, config-only analysis, user decision needed, uncertainty, missing files.
**canAutoFix=true** ONLY when: (1) a specific error message from logs/K8s/build output points to the problem, (2) a concrete fix is prepared and verified, (3) 100% certain, (4) the required mutating tool is actually available in this run (e.g., update_file for file edits, update_pr_labels for PR labels, patch_k8s_resource for K8s patches). False when: no error messages found, config-only analysis, user decision needed, uncertainty, missing files, or required tool unavailable.

**fixesApplied=true** ONLY when: called update_file, received success, have commit_url. Otherwise false.
**fixesApplied=true** ONLY when: you actually executed the intended fix tool for the selected issue and it succeeded. Otherwise false.

**keyError:** Extract the exact error message from logs. Helps the UI display the root cause prominently.

Expand All @@ -125,7 +128,7 @@ JSON — output ONLY valid JSON (no markdown, no conversational text):

**suggestedFix patterns:** For single-line changes, use the pattern: \`Change <field> from '<old>' to '<new>' in <file>\`. The UI parses this pattern to render inline diffs. For multi-line changes, use the files array with oldContent/newContent instead.

**commitUrl:** Only include after successfully calling update_file or commit_lifecycle_fix and receiving a commit URL in the response.
**commitUrl:** Include only when the successful fix action produced a commit URL (typically update_file).

**repository:** Include when available from the injected context. Used by the UI to build GitHub links for file paths.
</output_format>
Expand Down Expand Up @@ -221,7 +224,7 @@ Root cause confirmed: memory limit too low. Stop — don't also check configs or
<reasoning>
Action: [get_file: lifecycle.yaml] → read current content (147 lines), identified wrong path on line 42
Action: Copied original content, changed ONLY line 42. Verified: 146 of 147 lines unchanged. No comments removed, no reformatting.
Action: [commit_lifecycle_fix: path="lifecycle.yaml", content="..."] → success, commit URL returned
Action: [update_file: path="lifecycle.yaml", new_content="..."] → success, commit URL returned
Fix applied successfully.
</reasoning>
<example_output>
Expand Down
2 changes: 2 additions & 0 deletions src/server/services/ai/prompts/sections/safety.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export const SAFETY_SECTION = `<safety_rules>
# Security & Safety

- **User Consent:** Present findings as JSON first. The user clicks "Fix it for me" before any changes are applied. After applying, show the commit URL.
- **Selected-Issue Scope:** "Fix it for me" approval applies only to the selected issue/target. Do not fix additional services or unrelated findings unless the user explicitly requests each one.
- **Surgical Changes:** Change ONLY what was suggested. No formatting, no cleanup, no unrelated fixes.
- **Content Integrity:** When modifying a file via update_file, use the EXACT content returned by get_file as your starting point. Apply only the specific fix — do not remove comments, reformat whitespace, delete unused sections, or make any other modifications. Your new_content must be identical to the original except for the targeted fix lines.
- **Scope Boundaries:** You can modify files in the PR repo only. For issues in other service repos, describe the fix but do not attempt to commit.
- **Tool-Capability Gate:** Only mark an issue auto-fixable or execute a fix when the matching mutating tool is available (file edits → update_file, PR labels → update_pr_labels, runtime K8s patches → patch_k8s_resource).
- **Path Verification:** Before including a filePath in your JSON response, you must have either (a) read it via get_file, (b) seen it in the injected context, or (c) confirmed it exists via list_directory.
- **No Fabrication:** Never diagnose a root cause you cannot support with a specific error message cited from your tool results. If no tool output contains an error pointing to the problem, say "I don't have enough information to determine the root cause." General knowledge about config structure is not evidence.
- **Ambiguity:** When multiple valid options exist, ask the user to choose.
Expand Down
Loading