homeagent-runner 0.0.0-stage → 0.4.4

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/CONTRIBUTING.md +65 -0
  3. package/LICENSE +21 -0
  4. package/README.en.md +300 -0
  5. package/README.md +217 -2
  6. package/SECURITY.md +59 -0
  7. package/THIRD_PARTY_NOTICES.md +45 -0
  8. package/bin/runner.mjs +2 -0
  9. package/dist/runner/agent.js +254 -0
  10. package/dist/runner/cli.js +66 -0
  11. package/dist/runner/config-location.js +67 -0
  12. package/dist/runner/config.js +43 -0
  13. package/dist/runner/context.js +86 -0
  14. package/dist/runner/credentials.js +68 -0
  15. package/dist/runner/editor.js +262 -0
  16. package/dist/runner/files.js +298 -0
  17. package/dist/runner/git.js +333 -0
  18. package/dist/runner/lock.js +28 -0
  19. package/dist/runner/output-log.js +59 -0
  20. package/dist/runner/patch.js +103 -0
  21. package/dist/runner/permissions.js +93 -0
  22. package/dist/runner/policy.js +87 -0
  23. package/dist/runner/process-tree.js +28 -0
  24. package/dist/runner/processes.js +367 -0
  25. package/dist/runner/service.js +53 -0
  26. package/dist/runner/setup.js +79 -0
  27. package/dist/runner/tools.js +118 -0
  28. package/dist/shared/permissions.js +40 -0
  29. package/dist/shared/protocol.js +98 -0
  30. package/dist/shared/tools.js +108 -0
  31. package/docs/ARCHITECTURE.md +92 -0
  32. package/docs/CLIENTS.md +130 -0
  33. package/docs/COMPARISON.md +36 -0
  34. package/docs/DEPLOYMENT.md +295 -0
  35. package/docs/DEVELOPMENT_WORKFLOW.md +126 -0
  36. package/docs/OPEN_SOURCE.md +127 -0
  37. package/docs/README.md +26 -0
  38. package/docs/TOOLS.md +105 -0
  39. package/docs/VALIDATION.md +172 -0
  40. package/docs/WEBCODEX_CODE_AUDIT.md +107 -0
  41. package/docs/webcodex-parity-map.json +75 -0
  42. package/docs/webcodex-source-inventory.json +2254 -0
  43. package/licenses/npm-runtime.txt +3673 -0
  44. package/package.json +44 -4
  45. package/runtime/windows-process-tree.cs +105 -0
  46. package/runtime/windows-process-tree.ps1 +19 -0
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "homeagent-runner",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "description": "HomeAgent outbound development runner",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/jhxxr/HomeAgent.git"
8
+ },
9
+ "homepage": "https://github.com/jhxxr/HomeAgent#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/jhxxr/HomeAgent/issues"
12
+ },
13
+ "keywords": [
14
+ "remote-development",
15
+ "runner",
16
+ "automation"
17
+ ],
18
+ "engines": {
19
+ "node": ">=24.14.0"
20
+ },
21
+ "bin": {
22
+ "homeagent": "bin/runner.mjs"
23
+ },
24
+ "files": [
25
+ "bin/runner.mjs",
26
+ "dist/runner/**/*.js",
27
+ "dist/shared/**/*.js",
28
+ "runtime/",
29
+ "README.en.md",
30
+ "CONTRIBUTING.md",
31
+ "SECURITY.md",
32
+ "CHANGELOG.md",
33
+ "THIRD_PARTY_NOTICES.md",
34
+ "licenses/",
35
+ "docs/"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org/"
40
+ },
41
+ "dependencies": {
42
+ "ws": "8.22.0",
43
+ "zod": "4.6.7"
44
+ },
45
+ "version": "0.4.4"
46
+ }
@@ -0,0 +1,105 @@
1
+ using System;
2
+ using System.ComponentModel;
3
+ using System.Runtime.InteropServices;
4
+ using System.Text;
5
+
6
+ namespace HomeAgent {
7
+ // Assign a suspended child before user code runs. The Job owns descendants even after their leader exits.
8
+ public static class ProcessTree {
9
+ [StructLayout(LayoutKind.Sequential)] struct IO_COUNTERS { public ulong r, w, o, rb, wb, ob; }
10
+ [StructLayout(LayoutKind.Sequential)] struct LIMITS {
11
+ public long ProcessTime, JobTime; public uint Flags;
12
+ public UIntPtr MinWorkingSet, MaxWorkingSet; public uint ActiveProcesses;
13
+ public UIntPtr Affinity; public uint Priority, Scheduling;
14
+ }
15
+ [StructLayout(LayoutKind.Sequential)] struct EXTENDED_LIMITS {
16
+ public LIMITS Basic; public IO_COUNTERS IO;
17
+ public UIntPtr ProcessMemory, JobMemory, PeakProcessMemory, PeakJobMemory;
18
+ }
19
+ [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] struct STARTUPINFO {
20
+ public uint cb; public string reserved, desktop, title;
21
+ public uint x, y, width, height, cx, cy, fill, flags; public ushort show, reserved2;
22
+ public IntPtr reservedPtr, input, output, error;
23
+ }
24
+ [StructLayout(LayoutKind.Sequential)] struct PROCESS_INFORMATION { public IntPtr process, thread; public uint pid, tid; }
25
+ [StructLayout(LayoutKind.Sequential)] struct ACCOUNTING {
26
+ public long User, Kernel, PeriodUser, PeriodKernel;
27
+ public uint PageFaults, Processes, Active, Terminated;
28
+ }
29
+ [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)] static extern IntPtr CreateJobObjectW(IntPtr attributes, string name);
30
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool SetInformationJobObject(IntPtr job, int info, ref EXTENDED_LIMITS data, uint length);
31
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
32
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool TerminateJobObject(IntPtr job, uint code);
33
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool QueryInformationJobObject(IntPtr job, int info, out ACCOUNTING data, uint length, IntPtr returned);
34
+ [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)] static extern bool CreateProcessW(string application, StringBuilder command, IntPtr processAttributes, IntPtr threadAttributes, bool inherit, uint flags, IntPtr env, string cwd, ref STARTUPINFO info, out PROCESS_INFORMATION process);
35
+ [DllImport("kernel32.dll", SetLastError=true)] static extern uint ResumeThread(IntPtr thread);
36
+ [DllImport("kernel32.dll", SetLastError=true)] static extern IntPtr OpenProcess(uint access, bool inherit, uint pid);
37
+ [DllImport("kernel32.dll", SetLastError=true)] static extern uint WaitForMultipleObjects(uint count, IntPtr[] handles, bool all, uint timeout);
38
+ [DllImport("kernel32.dll", SetLastError=true)] static extern uint WaitForSingleObject(IntPtr handle, uint timeout);
39
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool GetExitCodeProcess(IntPtr process, out uint code);
40
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool TerminateProcess(IntPtr process, uint code);
41
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool CloseHandle(IntPtr handle);
42
+ [DllImport("kernel32.dll")] static extern IntPtr GetStdHandle(int id);
43
+ [DllImport("kernel32.dll", SetLastError=true)] static extern bool SetHandleInformation(IntPtr handle, uint mask, uint flags);
44
+ [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)] static extern uint SearchPathW(string path, string file, string extension, uint size, StringBuilder buffer, IntPtr filePart);
45
+ static void Check(bool ok) { if (!ok) throw new Win32Exception(Marshal.GetLastWin32Error()); }
46
+ static void TerminateTree(IntPtr job, uint code) {
47
+ Check(TerminateJobObject(job, code));
48
+ DateTime deadline = DateTime.UtcNow.AddSeconds(5);
49
+ while (true) {
50
+ ACCOUNTING accounting;
51
+ Check(QueryInformationJobObject(job, 1, out accounting, (uint)Marshal.SizeOf(typeof(ACCOUNTING)), IntPtr.Zero));
52
+ if (accounting.Active == 0) return;
53
+ if (DateTime.UtcNow >= deadline) throw new InvalidOperationException("Process tree did not finish terminating");
54
+ System.Threading.Thread.Sleep(10);
55
+ }
56
+ }
57
+ static string Quote(string value) {
58
+ if (value.IndexOf('\0') >= 0) throw new ArgumentException("NUL is not a valid argument");
59
+ StringBuilder output = new StringBuilder("\""); int slashes = 0;
60
+ foreach (char c in value) {
61
+ if (c == '\\') { slashes++; continue; }
62
+ if (c == '"') { output.Append('\\', slashes * 2 + 1); output.Append('"'); }
63
+ else { output.Append('\\', slashes); output.Append(c); }
64
+ slashes = 0;
65
+ }
66
+ output.Append('\\', slashes * 2); output.Append('"'); return output.ToString();
67
+ }
68
+ public static int Run(string executable, string[] args, int parentPid) {
69
+ IntPtr job = IntPtr.Zero, parent = IntPtr.Zero; PROCESS_INFORMATION child = new PROCESS_INFORMATION();
70
+ bool assigned = false;
71
+ try {
72
+ parent = OpenProcess(0x00100000, false, (uint)parentPid);
73
+ if (parent == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
74
+ if (WaitForSingleObject(parent, 0) == 0) throw new InvalidOperationException("Runner exited before process admission");
75
+ job = CreateJobObjectW(IntPtr.Zero, null);
76
+ if (job == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
77
+ EXTENDED_LIMITS limits = new EXTENDED_LIMITS(); limits.Basic.Flags = 0x00002000;
78
+ Check(SetInformationJobObject(job, 9, ref limits, (uint)Marshal.SizeOf(typeof(EXTENDED_LIMITS))));
79
+ StringBuilder resolved = new StringBuilder(32768);
80
+ uint length = SearchPathW(null, executable, ".exe", (uint)resolved.Capacity, resolved, IntPtr.Zero);
81
+ if (length == 0 || length >= resolved.Capacity) throw new Win32Exception(Marshal.GetLastWin32Error());
82
+ StringBuilder command = new StringBuilder(Quote(resolved.ToString()));
83
+ foreach (string arg in args) command.Append(" ").Append(Quote(arg));
84
+ STARTUPINFO startup = new STARTUPINFO(); startup.cb = (uint)Marshal.SizeOf(typeof(STARTUPINFO));
85
+ startup.flags = 0x00000100; startup.input = GetStdHandle(-10); startup.output = GetStdHandle(-11); startup.error = GetStdHandle(-12);
86
+ Check(SetHandleInformation(startup.input, 1, 1)); Check(SetHandleInformation(startup.output, 1, 1)); Check(SetHandleInformation(startup.error, 1, 1));
87
+ Check(CreateProcessW(resolved.ToString(), command, IntPtr.Zero, IntPtr.Zero, true, 0x08000004, IntPtr.Zero, Environment.CurrentDirectory, ref startup, out child));
88
+ Check(AssignProcessToJobObject(job, child.process)); assigned = true;
89
+ if (ResumeThread(child.thread) == 0xFFFFFFFF) throw new Win32Exception(Marshal.GetLastWin32Error());
90
+ uint wait = WaitForMultipleObjects(2, new IntPtr[] { parent, child.process }, false, 0xFFFFFFFF);
91
+ if (wait == 0) { TerminateTree(job, 130); return 130; }
92
+ if (wait != 1) throw new Win32Exception(Marshal.GetLastWin32Error());
93
+ uint code; Check(GetExitCodeProcess(child.process, out code));
94
+ TerminateTree(job, code);
95
+ return unchecked((int)code);
96
+ } finally {
97
+ if (!assigned && child.process != IntPtr.Zero) TerminateProcess(child.process, 127);
98
+ if (child.thread != IntPtr.Zero) CloseHandle(child.thread);
99
+ if (child.process != IntPtr.Zero) CloseHandle(child.process);
100
+ if (job != IntPtr.Zero) CloseHandle(job);
101
+ if (parent != IntPtr.Zero) CloseHandle(parent);
102
+ }
103
+ }
104
+ }
105
+ }
@@ -0,0 +1,19 @@
1
+ param(
2
+ [Parameter(Mandatory=$true)][string]$Executable,
3
+ [Parameter(Mandatory=$true)][string]$ArgumentsBase64,
4
+ [Parameter(Mandatory=$true)][int]$ParentPid
5
+ )
6
+ $ErrorActionPreference = 'Stop'
7
+ try {
8
+ # Import the built-in module explicitly instead of scanning arbitrary module paths.
9
+ $utilityModule = [System.IO.Path]::Combine($PSHOME, 'Modules', 'Microsoft.PowerShell.Utility', 'Microsoft.PowerShell.Utility.psd1')
10
+ Import-Module -Name $utilityModule -ErrorAction Stop
11
+ Add-Type -Path ([System.IO.Path]::Combine($PSScriptRoot, 'windows-process-tree.cs'))
12
+ $taskArgs = ConvertFrom-Json -InputObject ([System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($ArgumentsBase64)))
13
+ if ($null -eq $taskArgs) { $taskArgs = @() }
14
+ $taskExitCode = [HomeAgent.ProcessTree]::Run($Executable, [string[]]$taskArgs, $ParentPid)
15
+ exit $taskExitCode
16
+ } catch {
17
+ [Console]::Error.WriteLine('HomeAgent process supervisor failed: ' + $_.Exception.Message)
18
+ exit 127
19
+ }