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
42 changes: 32 additions & 10 deletions apps/twig/src/main/services/agent/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { MAIN_TOKENS } from "../../di/tokens.js";
import { logger } from "../../lib/logger.js";
import { TypedEventEmitter } from "../../lib/typed-event-emitter.js";
import type { FsService } from "../fs/service.js";
import { getCurrentUserId, getPostHogClient } from "../posthog-analytics.js";
import type { ProcessTrackingService } from "../process-tracking/service.js";
import type { SleepService } from "../sleep/service.js";
import {
Expand Down Expand Up @@ -437,23 +438,30 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
const mockNodeDir = this.setupMockNodeEnvironment(taskRunId);
this.setupEnvironment(credentials, mockNodeDir);

// Route agent logs to dedicated agent_logs Kafka topic via capture-logs-agent service
// In local dev, use Caddy proxy (port 8010) which routes /i/v1/agent-logs to capture-logs-agent
// In prod, use the main API host which proxies /i/v1/agent-logs to capture-logs-agent
const otelHost = credentials.apiHost;
const otelPath = "/i/v1/agent-logs";
// OTEL log pipeline or legacy S3 writer if FF false
const useOtelPipeline = await this.isFeatureFlagEnabled(
"twig-agent-logs-pipeline",
);

log.info("Agent log transport", {
transport: useOtelPipeline ? "otel" : "s3",
taskId,
taskRunId,
});

const agent = new Agent({
posthog: {
apiUrl: credentials.apiHost,
getApiKey: () => this.getToken(credentials.apiKey),
projectId: credentials.projectId,
},
otelTransport: {
host: otelHost,
apiKey: this.getToken(credentials.apiKey),
logsPath: otelPath,
},
otelTransport: useOtelPipeline
? {
host: credentials.apiHost,
apiKey: this.getToken(credentials.apiKey),
logsPath: "/i/v1/agent-logs",
}
: undefined,
debug: !app.isPackaged,
onLog: onAgentLog,
});
Expand Down Expand Up @@ -956,6 +964,20 @@ For git operations while detached:
return mockNodeDir;
}

private async isFeatureFlagEnabled(flagKey: string): Promise<boolean> {
try {
const client = getPostHogClient();
const userId = getCurrentUserId();
if (!client || !userId) {
return false;
}
return (await client.isFeatureEnabled(flagKey, userId)) ?? false;
} catch (error) {
log.warn(`Error checking feature flag "${flagKey}":`, error);
return false;
}
}

private cleanupMockNodeEnvironment(mockNodeDir: string): void {
try {
rmSync(mockNodeDir, { recursive: true, force: true });
Expand Down
20 changes: 12 additions & 8 deletions packages/agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ export class Agent {
this.posthogAPI = new PostHogAPIClient(config.posthog);
}

if (config.posthog || config.otelTransport) {
if (config.otelTransport) {
// OTEL pipeline: use OtelLogWriter only (no S3 writer)
this.sessionLogWriter = new SessionLogWriter({
otelConfig: {
posthogHost: config.otelTransport.host,
apiKey: config.otelTransport.apiKey,
logsPath: config.otelTransport.logsPath,
},
logger: this.logger.child("SessionLogWriter"),
});
} else if (config.posthog) {
// Legacy: use S3 writer via PostHog API
this.sessionLogWriter = new SessionLogWriter({
posthogAPI: this.posthogAPI,
otelConfig: config.otelTransport
? {
posthogHost: config.otelTransport.host,
apiKey: config.otelTransport.apiKey,
logsPath: config.otelTransport.logsPath,
}
: undefined,
logger: this.logger.child("SessionLogWriter"),
});
}
Expand Down
Loading