/ opencodehub / src / runner / executor.ts
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
255 lines · 9.0 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

import Docker from 'dockerode';
import pino from 'pino';
import { Writable } from 'stream';

const logger = pino({
    name: 'runner-executor',
    transport: {
        target: 'pino-pretty',
        options: { colorize: true }
    }
});

// Default resource limits for job containers
const DEFAULT_LIMITS = {
    memoryMb: parseInt(process.env.EXECUTOR_MEMORY_MB || "2048", 10),
    cpuShares: parseInt(process.env.EXECUTOR_CPU_SHARES || "1024", 10),
    cpuCount: parseInt(process.env.EXECUTOR_CPU_COUNT || "2", 10), // NanoCpus
    timeoutSeconds: parseInt(process.env.EXECUTOR_TIMEOUT_SECONDS || "3600", 10),
    maxLogBytes: parseInt(process.env.EXECUTOR_MAX_LOG_BYTES || String(10 * 1024 * 1024), 10),
};

// Patterns for secrets that should be redacted from logs
const SECRET_PATTERNS = [
    /(?:password|secret|token|api[_-]?key|auth|credential|bearer)[\s]*[=:]\s*['"]?([^'"\s]+)['"]?/gi,
    /ghp_[a-zA-Z0-9]{36}/g,               // GitHub personal access tokens
    /sk-[a-zA-Z0-9]{48}/g,                 // OpenAI API keys
    /sk-ant-[a-zA-Z0-9-]{93}/g,            // Anthropic API keys
    /xox[baprs]-[a-zA-Z0-9-]+/g,           // Slack tokens
    /eyJ[a-zA-Z0-9_-]*\.eyJ[a-zA-Z0-9_-]*/g, // JWT tokens
];

export interface ExecutorOptions {
    memoryMb?: number;
    cpuShares?: number;
    cpuCount?: number;
    timeoutSeconds?: number;
    maxLogBytes?: number;
    env?: Record<string, string>;
    workDir?: string;
}

export class Executor {
    private docker: Docker;

    constructor() {
        this.docker = new Docker();
    }

    async verifyDocker() {
        try {
            await this.docker.ping();
            logger.info("Docker is available");
            return true;
        } catch (e) {
            logger.error("Docker is NOT available. Please install Docker or ensure the runner has permissions.");
            return false;
        }
    }

    /**
     * Redact secrets from log output
     */
    private redactSecrets(text: string): string {
        let redacted = text;
        for (const pattern of SECRET_PATTERNS) {
            redacted = redacted.replace(pattern, (match) => {
                // Keep first 4 chars visible for debugging
                const visible = match.substring(0, 4);
                return `${visible}${'*'.repeat(Math.max(4, match.length - 4))}[REDACTED]`;
            });
        }
        return redacted;
    }

    /**
     * Run a job with resource limits, timeout, and log sanitization
     */
    async runJob(
        jobId: string,
        script: string,
        image = 'node:20-slim',
        options: ExecutorOptions = {}
    ): Promise<{ success: boolean; logs: string; exitCode: number; timedOut?: boolean }> {
        const containerName = `och-runner-${jobId}-${Date.now()}`;
        const limits = {
            memoryMb: options.memoryMb ?? DEFAULT_LIMITS.memoryMb,
            cpuShares: options.cpuShares ?? DEFAULT_LIMITS.cpuShares,
            timeoutSeconds: options.timeoutSeconds ?? DEFAULT_LIMITS.timeoutSeconds,
            maxLogBytes: options.maxLogBytes ?? DEFAULT_LIMITS.maxLogBytes,
        };

        let logs = '';
        let logBytes = 0;
        let logsTruncated = false;
        let container: Docker.Container | null = null;

        const logStream = new Writable({
            write: (chunk, encoding, callback) => {
                const chunkStr = chunk.toString();
                const redactedChunk = this.redactSecrets(chunkStr);

                // Check log size limit
                if (logBytes + redactedChunk.length > limits.maxLogBytes) {
                    if (!logsTruncated) {
                        logs += '\n[LOG TRUNCATED: Exceeded maximum log size]\n';
                        logsTruncated = true;
                    }
                } else {
                    logs += redactedChunk;
                    logBytes += redactedChunk.length;
                    // Stream to stdout for visibility (also redacted)
                    process.stdout.write(redactedChunk);
                }
                callback();
            }
        });

        // Setup timeout
        const timeoutPromise = new Promise<{ timedOut: true }>((resolve) => {
            setTimeout(() => {
                resolve({ timedOut: true });
            }, limits.timeoutSeconds * 1000);
        });

        try {
            logger.info({
                image,
                containerName,
                memoryMb: limits.memoryMb,
                cpuShares: limits.cpuShares,
                timeoutSeconds: limits.timeoutSeconds
            }, "Starting job container with resource limits");

            container = await this.docker.createContainer({
                Image: image,
                Cmd: ['/bin/sh', '-c', script],
                Tty: false,
                name: containerName,
                Env: options.env ? Object.entries(options.env).map(([k, v]) => `${k}=${v}`) : undefined,
                WorkingDir: options.workDir,
                HostConfig: {
                    // Memory limit in bytes
                    Memory: limits.memoryMb * 1024 * 1024,
                    // Memory + swap limit (same as Memory to disable swap)
                    MemorySwap: limits.memoryMb * 1024 * 1024,
                    // CPU shares (relative weight)
                    CpuShares: limits.cpuShares,
                    // Limit CPUs (NanoCpus is int64, unit is 1e-9 CPUs)
                    NanoCpus: (options.cpuCount ?? DEFAULT_LIMITS.cpuCount) * 1e9,
                    // Security: prevent container from gaining new privileges
                    SecurityOpt: ['no-new-privileges:true'],
                    // Read-only root filesystem (can be overridden if needed)
                    // ReadonlyRootfs: true,
                    // Disable network by default for security (uncomment if needed)
                    // NetworkMode: 'none',
                    // Auto-remove on stop
                    AutoRemove: false, // We remove manually after getting logs
                    // Limit PIDs to prevent fork bombs
                    PidsLimit: 256,
                }
            });

            const stream = await container.attach({
                stream: true,
                stdout: true,
                stderr: true
            });

            stream.pipe(logStream);

            await container.start();

            // Race between completion and timeout
            const waitPromise = container.wait().then(result => ({ result, timedOut: false }));
            const outcome = await Promise.race([waitPromise, timeoutPromise]);

            if ('timedOut' in outcome && outcome.timedOut) {
                logger.warn({ jobId, containerName, timeoutSeconds: limits.timeoutSeconds },
                    "Job timed out, killing container");

                try {
                    await container.kill();
                } catch (killError) {
                    logger.error({ error: killError }, "Failed to kill timed-out container");
                }

                logs += `\n[JOB TIMED OUT after ${limits.timeoutSeconds} seconds]\n`;

                return {
                    success: false,
                    exitCode: 124, // Standard timeout exit code
                    logs,
                    timedOut: true
                };
            }

            const { result } = outcome as { result: { StatusCode: number }; timedOut: false };

            // Small delay to ensure logs flush
            await new Promise(r => setTimeout(r, 500));

            await container.remove();

            return {
                success: result.StatusCode === 0,
                exitCode: result.StatusCode,
                logs
            };

        } catch (error: any) {
            logger.error(error, "Job execution failed");

            // Attempt to clean up container on error
            if (container) {
                try {
                    await container.stop({ t: 5 }).catch(() => { });
                    await container.remove().catch(() => { });
                } catch {
                    // Ignore cleanup errors
                }
            }

            return {
                success: false,
                exitCode: 1,
                logs: logs + `\nRunner System Error: ${this.redactSecrets(error.message)}`
            };
        }
    }

    /**
     * Cancel a running job by container name pattern
     */
    async cancelJob(jobId: string): Promise<boolean> {
        try {
            const containers = await this.docker.listContainers({
                filters: { name: [`och-runner-${jobId}`] }
            });

            for (const containerInfo of containers) {
                const container = this.docker.getContainer(containerInfo.Id);
                logger.info({ jobId, containerId: containerInfo.Id }, "Cancelling job");
                await container.stop({ t: 5 });
                await container.remove();
            }

            return containers.length > 0;
        } catch (error) {
            logger.error({ error, jobId }, "Failed to cancel job");
            return false;
        }
    }
}