Skip to content

Commit 2d9873b

Browse files
committed
refactor: migrate attach socket paths to TypeScript
1 parent b4bd5e2 commit 2d9873b

4 files changed

Lines changed: 23 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ src/agent/critical-agent-policy.js
159159
src/agent/provider-control-plane.js
160160
src/agent/structured-output-error.js
161161
src/agent/validation-platform.js
162+
src/attach/socket-paths.js
162163
src/providers/anthropic/index.js
163164
src/providers/capabilities.js
164165
src/providers/google/index.js

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ export default [
413413
'src/agent/provider-control-plane.js',
414414
'src/agent/structured-output-error.js',
415415
'src/agent/validation-platform.js',
416+
'src/attach/socket-paths.js',
416417
'src/providers/anthropic/index.js',
417418
'src/providers/capabilities.js',
418419
'src/providers/google/index.js',
Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
1-
/**
2-
* Deterministic attach socket paths.
3-
*
4-
* Unix-domain sockets have a small path budget (104 bytes on macOS). Keep the
5-
* live socket namespace independent from HOME while retaining one namespace
6-
* per OS user and Zeroshot home.
7-
*/
8-
9-
const crypto = require('crypto');
10-
const fs = require('fs');
11-
const os = require('os');
12-
const path = require('path');
1+
import crypto = require('crypto');
2+
import fs = require('fs');
3+
import os = require('os');
4+
import path = require('path');
135

146
const SOCKET_ROOT = process.platform === 'win32' ? null : '/tmp';
157
const SOCKET_DIR_MODE = 0o700;
168

17-
function shortHash(value) {
9+
function shortHash(value: string): string {
1810
return crypto.createHash('sha256').update(value).digest('hex').slice(0, 16);
1911
}
2012

21-
function userNamespace() {
13+
function userNamespace(): string {
2214
if (typeof process.getuid === 'function') {
2315
return String(process.getuid());
2416
}
2517
return shortHash(os.userInfo().username);
2618
}
2719

28-
function resolveHomeDir(env = process.env) {
20+
function resolveHomeDir(env: NodeJS.ProcessEnv = process.env): string {
2921
return env.ZEROSHOT_HOME || env.HOME || env.USERPROFILE || os.homedir();
3022
}
3123

32-
function getSocketDir(homeDir = resolveHomeDir()) {
33-
if (process.platform === 'win32') {
24+
function getSocketDir(homeDir = resolveHomeDir()): string {
25+
if (SOCKET_ROOT === null) {
3426
return path.join(homeDir, '.zeroshot', 'sockets');
3527
}
3628
return path.join(SOCKET_ROOT, `zeroshot-${userNamespace()}-${shortHash(homeDir)}`);
3729
}
3830

39-
function assertSafeSocketDir(socketDir) {
31+
function assertSafeSocketDir(socketDir: string): void {
4032
const stat = fs.lstatSync(socketDir);
4133
if (!stat.isDirectory() || stat.isSymbolicLink()) {
4234
throw new Error(`Attach socket path is not a directory: ${socketDir}`);
@@ -46,7 +38,7 @@ function assertSafeSocketDir(socketDir) {
4638
}
4739
}
4840

49-
function ensureOwnedDirectory(socketDir) {
41+
function ensureOwnedDirectory(socketDir: string): string {
5042
fs.mkdirSync(socketDir, { recursive: true, mode: SOCKET_DIR_MODE });
5143
assertSafeSocketDir(socketDir);
5244
if (process.platform !== 'win32') {
@@ -55,26 +47,30 @@ function ensureOwnedDirectory(socketDir) {
5547
return socketDir;
5648
}
5749

58-
function ensureSocketDir(homeDir = resolveHomeDir()) {
50+
function ensureSocketDir(homeDir = resolveHomeDir()): string {
5951
const socketDir = getSocketDir(homeDir);
6052
return ensureOwnedDirectory(socketDir);
6153
}
6254

63-
function getTaskSocketPath(taskId, homeDir = resolveHomeDir()) {
55+
function getTaskSocketPath(taskId: string, homeDir = resolveHomeDir()): string {
6456
return path.join(ensureSocketDir(homeDir), `${taskId}.sock`);
6557
}
6658

67-
function getAgentSocketPath(clusterId, agentId, homeDir = resolveHomeDir()) {
59+
function getAgentSocketPath(
60+
clusterId: string,
61+
agentId: string,
62+
homeDir = resolveHomeDir()
63+
): string {
6864
const clusterDir = path.join(ensureSocketDir(homeDir), clusterId);
6965
ensureOwnedDirectory(clusterDir);
7066
return path.join(clusterDir, `${agentId}.sock`);
7167
}
7268

73-
function getClusterSocketPath(clusterId, homeDir = resolveHomeDir()) {
69+
function getClusterSocketPath(clusterId: string, homeDir = resolveHomeDir()): string {
7470
return path.join(ensureSocketDir(homeDir), `${clusterId}.sock`);
7571
}
7672

77-
module.exports = {
73+
export = {
7874
SOCKET_DIR_MODE,
7975
resolveHomeDir,
8076
getSocketDir,

tests/helpers/runtime-typescript-package-assertions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const RUNTIME_TYPESCRIPT_OUTPUTS = Object.freeze([
2121
'src/agent/provider-control-plane.js',
2222
'src/agent/structured-output-error.js',
2323
'src/agent/validation-platform.js',
24+
'src/attach/socket-paths.js',
2425
'src/providers/anthropic/index.js',
2526
'src/providers/capabilities.js',
2627
'src/providers/google/index.js',

0 commit comments

Comments
 (0)