@online-tools/work-agent-runner 0.0.3-alpha-1 → 0.0.5-alpha-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,15 @@ export function renameTokenVariable(text) {
22
22
  return String(text).split('GITCODE_TOKEN').join('GITCODE_CRED');
23
23
  }
24
24
 
25
- /** Points the Skills' scratch directory `/tmp/work-agent` at the run's own directory. */
26
- export function rewriteWorkAgentPath(text, replacement) {
27
- return String(text).split('/tmp/work-agent').join(replacement);
25
+ /**
26
+ * Points the Skills' `/tmp/work-agent` at this machine's directories:
27
+ * `/tmp/work-agent/logs` at `logs` (kept after the job, $WORK_AGENT_HOME/logs)
28
+ * and everything else under it — clones, intermediate files — at `scratch`
29
+ * (the job's own directory, deleted with it). A job may write nowhere else:
30
+ * mapping it to $WORK_AGENT_HOME itself put the clones where the sandbox lets
31
+ * no job write. Paths that merely start with the same letters
32
+ * (`/tmp/work-agent-runner`) are left alone.
33
+ */
34
+ export function rewriteWorkAgentPath(text, { logs, scratch }) {
35
+ return String(text).replace(/\/tmp\/work-agent(\/logs)?(?![A-Za-z0-9_.-])/g, (_, inLogs) => (inLogs ? logs : scratch));
28
36
  }
@@ -25,10 +25,14 @@ export const MAX_TOTAL_BYTES = 512 * 1024 * 1024;
25
25
  * - a directory reached again through a symlink loop is not entered twice
26
26
  * `onFile(relPath, absPath, stats)` gets every regular file; `onDir(relPath,
27
27
  * absPath)` every directory ('' is the root) before its contents. Relative
28
- * paths use '/'.
28
+ * paths use '/'. With `boundary` (a real path), a symlink whose target lies
29
+ * outside it is skipped and reported to `onSkip(relPath, target)`: a link in
30
+ * the work-agent clone to ../config.json must not carry the machine token into
31
+ * a job's directory.
29
32
  */
30
- export function walkSkillTree(sourceDir, onFile, onDir = () => {}) {
33
+ export function walkSkillTree(sourceDir, onFile, onDir = () => {}, { boundary = '', onSkip = () => {} } = {}) {
31
34
  const seen = new Set();
35
+ const inside = (target) => target === boundary || target.startsWith(boundary.endsWith(path.sep) ? boundary : `${boundary}${path.sep}`);
32
36
  const walk = (dir, rel) => {
33
37
  const real = fs.realpathSync(dir);
34
38
  if (seen.has(real)) return; // symlink loop
@@ -38,6 +42,18 @@ export function walkSkillTree(sourceDir, onFile, onDir = () => {}) {
38
42
  if (entry.name === '.git') continue;
39
43
  const from = path.join(dir, entry.name);
40
44
  const childRel = rel ? `${rel}/${entry.name}` : entry.name;
45
+ if (boundary && entry.isSymbolicLink()) {
46
+ let target;
47
+ try {
48
+ target = fs.realpathSync(from);
49
+ } catch {
50
+ continue; // dangling symlink
51
+ }
52
+ if (!inside(target)) {
53
+ onSkip(childRel, target);
54
+ continue;
55
+ }
56
+ }
41
57
  let stats;
42
58
  try {
43
59
  stats = fs.statSync(from); // follows symlinks
@@ -58,11 +74,12 @@ export function walkSkillTree(sourceDir, onFile, onDir = () => {}) {
58
74
  * walkSkillTree for what is followed and skipped). The directory layout is
59
75
  * preserved, so relative references between Skills (`../gitcode-toolkit/...`)
60
76
  * keep working. Throws when the tree is larger than the limits above.
77
+ * `options.boundary` / `options.onSkip`: see walkSkillTree.
61
78
  */
62
- export function copyTreeDetached(source, target, limits = {}) {
63
- const maxFiles = limits.maxFiles ?? MAX_FILES;
64
- const maxFileBytes = limits.maxFileBytes ?? MAX_FILE_BYTES;
65
- const maxTotalBytes = limits.maxTotalBytes ?? MAX_TOTAL_BYTES;
79
+ export function copyTreeDetached(source, target, options = {}) {
80
+ const maxFiles = options.maxFiles ?? MAX_FILES;
81
+ const maxFileBytes = options.maxFileBytes ?? MAX_FILE_BYTES;
82
+ const maxTotalBytes = options.maxTotalBytes ?? MAX_TOTAL_BYTES;
66
83
  let files = 0;
67
84
  let bytes = 0;
68
85
  walkSkillTree(
@@ -78,21 +95,25 @@ export function copyTreeDetached(source, target, limits = {}) {
78
95
  fs.chmodSync(to, (stats.mode & 0o777) | 0o600);
79
96
  },
80
97
  (rel) => fs.mkdirSync(path.join(target, rel), { recursive: true, mode: 0o700 }),
98
+ { boundary: options.boundary || '', onSkip: options.onSkip },
81
99
  );
82
100
  return { files, bytes };
83
101
  }
84
102
 
85
- /** Points `/tmp/work-agent` at the run's scratch dir and renames GITCODE_TOKEN (AGENTS.md 5.1). */
86
- export function rewriteSkillPaths(directory, replacement) {
103
+ /**
104
+ * Points `/tmp/work-agent` at `targets` ({ logs, scratch }: skill-text.mjs
105
+ * rewriteWorkAgentPath) and renames GITCODE_TOKEN (AGENTS.md 5.1).
106
+ */
107
+ export function rewriteSkillPaths(directory, targets) {
87
108
  for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
88
109
  const target = path.join(directory, entry.name);
89
110
  if (entry.isDirectory()) {
90
- rewriteSkillPaths(target, replacement);
111
+ rewriteSkillPaths(target, targets);
91
112
  continue;
92
113
  }
93
114
  if (!entry.isFile() || !isSkillTextFile(entry.name)) continue;
94
115
  const text = fs.readFileSync(target, 'utf8');
95
116
  if (!text.includes('/tmp/work-agent') && !text.includes('GITCODE_TOKEN')) continue;
96
- fs.writeFileSync(target, renameTokenVariable(rewriteWorkAgentPath(text, replacement)));
117
+ fs.writeFileSync(target, renameTokenVariable(rewriteWorkAgentPath(text, targets)));
97
118
  }
98
119
  }