@muggleai/works 5.6.0 → 5.8.0
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.
- package/dist/{chunk-LTULYOOW.js → chunk-5PYK7DWB.js} +15 -0
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/dist/plugin/.claude-plugin/plugin.json +1 -1
- package/dist/plugin/.cursor-plugin/plugin.json +1 -1
- package/dist/plugin/hooks/README.md +15 -3
- package/dist/plugin/hooks/hooks.json +41 -1
- package/dist/plugin/scripts/gc-state.sh +66 -0
- package/dist/plugin/scripts/guard-run-job-object.ps1 +256 -0
- package/dist/plugin/scripts/guard-run.mjs +327 -0
- package/dist/plugin/scripts/guardrail-offer-ran.sh +35 -0
- package/dist/plugin/scripts/guardrail-pr-terminal.sh +21 -0
- package/dist/plugin/scripts/guardrail-record-tests.sh +5 -4
- package/dist/plugin/scripts/guardrail-terminal-gate.sh +38 -0
- package/dist/plugin/scripts/guardrails.mjs +91 -3
- package/dist/plugin/scripts/pr-watch-guards.sh +46 -0
- package/dist/plugin/skills/_shared/pr-followup-helpers/loop-signature.md +3 -3
- package/dist/plugin/skills/_shared/vcs/CLAUDE.md +2 -1
- package/dist/plugin/skills/_shared/vcs/common/branch-standing.md +19 -0
- package/dist/plugin/skills/_shared/vcs/common/ci-rollup.md +16 -0
- package/dist/plugin/skills/_shared/vcs/common/push-to-branch.md +16 -0
- package/{plugin/skills/_shared/vcs/github → dist/plugin/skills/_shared/vcs/common}/verify-working-tree.md +4 -4
- package/dist/plugin/skills/_shared/vcs/github/pr-edit.md +2 -0
- package/dist/plugin/skills/_shared/vcs/github/reply-line-comment.md +2 -0
- package/dist/plugin/skills/_shared/vcs/github/signed-commits.md +28 -0
- package/dist/plugin/skills/_shared/vcs/github/top-level-comment.md +2 -0
- package/dist/plugin/skills/_shared/vcs/github/unresolved-threads.md +39 -39
- package/dist/plugin/skills/_shared/vcs/github.md +5 -2
- package/dist/plugin/skills/_shared/vcs/gitlab/loop-user-identity.md +1 -1
- package/dist/plugin/skills/_shared/vcs/gitlab/mr-create.md +1 -1
- package/dist/plugin/skills/_shared/vcs/gitlab/mr-discussions.md +1 -1
- package/dist/plugin/skills/_shared/vcs/gitlab/mr-metadata.md +2 -2
- package/dist/plugin/skills/_shared/vcs/gitlab/mr-pipeline.md +1 -1
- package/dist/plugin/skills/_shared/vcs/gitlab/reply-discussion.md +1 -1
- package/dist/plugin/skills/_shared/vcs/gitlab/resolve-discussion.md +1 -1
- package/dist/plugin/skills/_shared/vcs/gitlab/signed-commits.md +15 -0
- package/dist/plugin/skills/_shared/vcs/gitlab/unresolved-discussions.md +2 -2
- package/dist/plugin/skills/_shared/vcs/gitlab.md +6 -3
- package/dist/plugin/skills/_shared/vcs/post-signature.md +31 -0
- package/dist/plugin/skills/do/build.md +58 -56
- package/dist/plugin/skills/do/open-prs/forward.md +85 -84
- package/dist/plugin/skills/do/open-prs/update.md +3 -3
- package/dist/plugin/skills/do/per-comment-replies.md +2 -2
- package/dist/plugin/skills/do/resolve-conflicts.md +59 -58
- package/dist/plugin/skills/do/unit-tests.md +1 -1
- package/dist/plugin/skills/muggle-pr-followup/CLAUDE.md +1 -0
- package/dist/plugin/skills/muggle-pr-followup/SKILL.md +11 -4
- package/dist/plugin/skills/muggle-pr-followup/arm-watcher.md +29 -5
- package/dist/plugin/skills/muggle-pr-followup/bootstrap.md +3 -1
- package/dist/plugin/skills/muggle-pr-followup/contract.md +171 -158
- package/dist/plugin/skills/muggle-pr-followup/evals/evals.json +221 -101
- package/dist/plugin/skills/muggle-pr-followup/output-templates/inline-reply.md +2 -2
- package/dist/plugin/skills/muggle-pr-followup/output-templates/resolve-reminder.md +1 -1
- package/dist/plugin/skills/muggle-pr-followup/reconcile.md +9 -5
- package/dist/plugin/skills/muggle-pr-followup/state-schemas.md +191 -175
- package/dist/plugin/skills/muggle-pr-followup/stop.md +31 -0
- package/dist/plugin/skills/muggle-pr-visual-walkthrough/SKILL.md +14 -5
- package/dist/plugin/skills/muggle-test-prepare/steps/start-services.md +2 -2
- package/dist/release-manifest.json +4 -4
- package/package.json +6 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/.cursor-plugin/plugin.json +1 -1
- package/plugin/hooks/README.md +15 -3
- package/plugin/hooks/hooks.json +41 -1
- package/plugin/scripts/gc-state.sh +66 -0
- package/plugin/scripts/guard-run-job-object.ps1 +256 -0
- package/plugin/scripts/guard-run.mjs +327 -0
- package/plugin/scripts/guardrail-offer-ran.sh +35 -0
- package/plugin/scripts/guardrail-pr-terminal.sh +21 -0
- package/plugin/scripts/guardrail-record-tests.sh +5 -4
- package/plugin/scripts/guardrail-terminal-gate.sh +38 -0
- package/plugin/scripts/guardrails.mjs +91 -3
- package/plugin/scripts/pr-watch-guards.sh +46 -0
- package/plugin/skills/_shared/pr-followup-helpers/loop-signature.md +3 -3
- package/plugin/skills/_shared/vcs/CLAUDE.md +2 -1
- package/plugin/skills/_shared/vcs/common/branch-standing.md +19 -0
- package/plugin/skills/_shared/vcs/common/ci-rollup.md +16 -0
- package/plugin/skills/_shared/vcs/common/push-to-branch.md +16 -0
- package/{dist/plugin/skills/_shared/vcs/github → plugin/skills/_shared/vcs/common}/verify-working-tree.md +4 -4
- package/plugin/skills/_shared/vcs/github/pr-edit.md +2 -0
- package/plugin/skills/_shared/vcs/github/reply-line-comment.md +2 -0
- package/plugin/skills/_shared/vcs/github/signed-commits.md +28 -0
- package/plugin/skills/_shared/vcs/github/top-level-comment.md +2 -0
- package/plugin/skills/_shared/vcs/github/unresolved-threads.md +39 -39
- package/plugin/skills/_shared/vcs/github.md +5 -2
- package/plugin/skills/_shared/vcs/gitlab/loop-user-identity.md +1 -1
- package/plugin/skills/_shared/vcs/gitlab/mr-create.md +1 -1
- package/plugin/skills/_shared/vcs/gitlab/mr-discussions.md +1 -1
- package/plugin/skills/_shared/vcs/gitlab/mr-metadata.md +2 -2
- package/plugin/skills/_shared/vcs/gitlab/mr-pipeline.md +1 -1
- package/plugin/skills/_shared/vcs/gitlab/reply-discussion.md +1 -1
- package/plugin/skills/_shared/vcs/gitlab/resolve-discussion.md +1 -1
- package/plugin/skills/_shared/vcs/gitlab/signed-commits.md +15 -0
- package/plugin/skills/_shared/vcs/gitlab/unresolved-discussions.md +2 -2
- package/plugin/skills/_shared/vcs/gitlab.md +6 -3
- package/plugin/skills/_shared/vcs/post-signature.md +31 -0
- package/plugin/skills/do/build.md +58 -56
- package/plugin/skills/do/open-prs/forward.md +85 -84
- package/plugin/skills/do/open-prs/update.md +3 -3
- package/plugin/skills/do/per-comment-replies.md +2 -2
- package/plugin/skills/do/resolve-conflicts.md +59 -58
- package/plugin/skills/do/unit-tests.md +1 -1
- package/plugin/skills/muggle-pr-followup/CLAUDE.md +1 -0
- package/plugin/skills/muggle-pr-followup/SKILL.md +11 -4
- package/plugin/skills/muggle-pr-followup/arm-watcher.md +29 -5
- package/plugin/skills/muggle-pr-followup/bootstrap.md +3 -1
- package/plugin/skills/muggle-pr-followup/contract.md +171 -158
- package/plugin/skills/muggle-pr-followup/evals/evals.json +221 -101
- package/plugin/skills/muggle-pr-followup/output-templates/inline-reply.md +2 -2
- package/plugin/skills/muggle-pr-followup/output-templates/resolve-reminder.md +1 -1
- package/plugin/skills/muggle-pr-followup/reconcile.md +9 -5
- package/plugin/skills/muggle-pr-followup/state-schemas.md +191 -175
- package/plugin/skills/muggle-pr-followup/stop.md +31 -0
- package/plugin/skills/muggle-pr-visual-walkthrough/SKILL.md +14 -5
- package/plugin/skills/muggle-test-prepare/steps/start-services.md +2 -2
- package/dist/plugin/skills/_shared/vcs/github/push-to-branch.md +0 -10
- package/plugin/skills/_shared/vcs/github/push-to-branch.md +0 -10
package/plugin/hooks/hooks.json
CHANGED
|
@@ -15,6 +15,12 @@
|
|
|
15
15
|
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/reconcile-stale-watchers.sh\"",
|
|
16
16
|
"async": false,
|
|
17
17
|
"timeout": 10
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"type": "command",
|
|
21
|
+
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/gc-state.sh\"",
|
|
22
|
+
"async": false,
|
|
23
|
+
"timeout": 10
|
|
18
24
|
}
|
|
19
25
|
]
|
|
20
26
|
}
|
|
@@ -47,11 +53,17 @@
|
|
|
47
53
|
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-record-tests.sh\"",
|
|
48
54
|
"async": false,
|
|
49
55
|
"timeout": 10
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"type": "command",
|
|
59
|
+
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-pr-terminal.sh\"",
|
|
60
|
+
"async": false,
|
|
61
|
+
"timeout": 10
|
|
50
62
|
}
|
|
51
63
|
]
|
|
52
64
|
},
|
|
53
65
|
{
|
|
54
|
-
"matcher": "mcp__.*muggle.*(execute|replay)",
|
|
66
|
+
"matcher": "mcp__.*muggle.*(execute|replay|telemetry-skill-emit)",
|
|
55
67
|
"hooks": [
|
|
56
68
|
{
|
|
57
69
|
"type": "command",
|
|
@@ -60,6 +72,28 @@
|
|
|
60
72
|
"timeout": 10
|
|
61
73
|
}
|
|
62
74
|
]
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"matcher": "Monitor",
|
|
78
|
+
"hooks": [
|
|
79
|
+
{
|
|
80
|
+
"type": "command",
|
|
81
|
+
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-pr-terminal.sh\"",
|
|
82
|
+
"async": false,
|
|
83
|
+
"timeout": 10
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"matcher": "AskUserQuestion",
|
|
89
|
+
"hooks": [
|
|
90
|
+
{
|
|
91
|
+
"type": "command",
|
|
92
|
+
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-offer-ran.sh\"",
|
|
93
|
+
"async": false,
|
|
94
|
+
"timeout": 10
|
|
95
|
+
}
|
|
96
|
+
]
|
|
63
97
|
}
|
|
64
98
|
],
|
|
65
99
|
"Stop": [
|
|
@@ -70,6 +104,12 @@
|
|
|
70
104
|
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-e2e-gate.sh\"",
|
|
71
105
|
"async": false,
|
|
72
106
|
"timeout": 10
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"type": "command",
|
|
110
|
+
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-terminal-gate.sh\"",
|
|
111
|
+
"async": false,
|
|
112
|
+
"timeout": 10
|
|
73
113
|
}
|
|
74
114
|
]
|
|
75
115
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
set -uo pipefail
|
|
4
|
+
|
|
5
|
+
# Prune ephemeral muggle state that nothing else garbage-collects, so it does not
|
|
6
|
+
# grow without bound (observed: hundreds of dead per-session files). Runs from a
|
|
7
|
+
# SessionStart hook, TTL-gated to once/day unless MUGGLE_STATE_GC_FORCE=1. Silent
|
|
8
|
+
# and best-effort: it never blocks session start and never prints context.
|
|
9
|
+
#
|
|
10
|
+
# Collection is keyed on INACTIVITY, never creation age, so a session that lasts
|
|
11
|
+
# months is never collected while it is still in use:
|
|
12
|
+
#
|
|
13
|
+
# guardrails/<session>.json one per Claude session. An in-use session keeps
|
|
14
|
+
# rewriting it (guardrails.mjs on every guarded tool call) and refreshes it
|
|
15
|
+
# on resume (below), so its mtime tracks last activity — only a session gone
|
|
16
|
+
# quiet for the whole window is collected, and by then it has ended.
|
|
17
|
+
# muggle-do/sessions/<slug>/ a PR-follow-up slot. Pruned only once finalized
|
|
18
|
+
# (result.md present — the terminal marker); an OPEN slot, a PR watched for
|
|
19
|
+
# any length of time, has no result.md and is never touched.
|
|
20
|
+
#
|
|
21
|
+
# Age tests use `find -mmin/-mtime` (portable across GNU and BSD) rather than
|
|
22
|
+
# `date -r`, whose file-mtime meaning is GNU-only.
|
|
23
|
+
|
|
24
|
+
home="${HOME}"
|
|
25
|
+
guardrails_dir="${home}/.muggle-ai/guardrails"
|
|
26
|
+
sessions_dir="${home}/.muggle-ai/muggle-do/sessions"
|
|
27
|
+
marker_dir="${home}/.cache/muggle"
|
|
28
|
+
marker="${marker_dir}/state-gc-checked"
|
|
29
|
+
guardrails_ttl_days="${MUGGLE_GUARDRAILS_TTL_DAYS:-14}"
|
|
30
|
+
slot_ttl_days="${MUGGLE_SLOT_TTL_DAYS:-30}"
|
|
31
|
+
|
|
32
|
+
# Refresh this session's own guardrails file before anything else, every start
|
|
33
|
+
# (not behind the daily gate — a resume on an already-swept day must still mark
|
|
34
|
+
# the session live). This is what makes a long-lived or resumed session immune to
|
|
35
|
+
# the inactivity sweep, whatever its age. Filename sanitization mirrors
|
|
36
|
+
# guardrails.mjs fileFor().
|
|
37
|
+
session_id=$(cat 2>/dev/null | sed -n 's/.*"session_id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)
|
|
38
|
+
if [ -n "${session_id}" ]; then
|
|
39
|
+
safe_id="${session_id//[^A-Za-z0-9_-]/_}"
|
|
40
|
+
[ -f "${guardrails_dir}/${safe_id}.json" ] && touch "${guardrails_dir}/${safe_id}.json" 2>/dev/null || true
|
|
41
|
+
fi
|
|
42
|
+
|
|
43
|
+
# TTL gate: skip when the marker exists and is younger than 24h (1440 min).
|
|
44
|
+
if [ -z "${MUGGLE_STATE_GC_FORCE:-}" ] && [ -f "$marker" ] \
|
|
45
|
+
&& [ -z "$(find "$marker" -mmin +1440 2>/dev/null)" ]; then
|
|
46
|
+
exit 0
|
|
47
|
+
fi
|
|
48
|
+
|
|
49
|
+
if [ -d "$guardrails_dir" ]; then
|
|
50
|
+
find "$guardrails_dir" -maxdepth 1 -type f -name '*.json' \
|
|
51
|
+
-mtime "+${guardrails_ttl_days}" -delete 2>/dev/null || true
|
|
52
|
+
fi
|
|
53
|
+
|
|
54
|
+
if [ -d "$sessions_dir" ]; then
|
|
55
|
+
for slot in "$sessions_dir"/*/; do
|
|
56
|
+
[ -d "$slot" ] || continue
|
|
57
|
+
[ -f "${slot}result.md" ] || continue
|
|
58
|
+
if [ -n "$(find "${slot}result.md" -mtime "+${slot_ttl_days}" 2>/dev/null)" ]; then
|
|
59
|
+
rm -rf "$slot" 2>/dev/null || true
|
|
60
|
+
fi
|
|
61
|
+
done
|
|
62
|
+
fi
|
|
63
|
+
|
|
64
|
+
mkdir -p "$marker_dir" 2>/dev/null || true
|
|
65
|
+
touch "$marker" 2>/dev/null || true
|
|
66
|
+
exit 0
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# Windows backend for guard-run. Callers never invoke this file: the portable
|
|
2
|
+
# entry point is guard-run.mjs (plain node, runs everywhere), which probes the
|
|
3
|
+
# platform and dispatches to one backend — this Job Object shim on win32,
|
|
4
|
+
# systemd scope / prlimit on linux, ulimit + process group on darwin. So skill
|
|
5
|
+
# instructions stay OS-agnostic by referencing guard-run.mjs only; PowerShell
|
|
6
|
+
# exists solely inside this win32 backend. guard-run.mjs invokes it as:
|
|
7
|
+
#
|
|
8
|
+
# powershell -File guard-run-job-object.ps1 <limit> <kill|persist> <launcherPid> <base64CommandLine>
|
|
9
|
+
#
|
|
10
|
+
# The child is created CREATE_SUSPENDED, assigned to a fresh Job Object, then
|
|
11
|
+
# resumed — so it and every descendant are inside the job from their first
|
|
12
|
+
# instruction. JOB_OBJECT_LIMIT_ACTIVE_PROCESS makes the process past the limit
|
|
13
|
+
# fail at creation in the kernel (nothing to clean up), which is what stops a
|
|
14
|
+
# fork storm: a sweeper that reacts after enumeration always loses that race.
|
|
15
|
+
#
|
|
16
|
+
# kill mode adds JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE — this shim holds the only
|
|
17
|
+
# job handle, so when it exits or is killed the kernel destroys the entire tree
|
|
18
|
+
# — and watches the launcher: if the node launcher dies first, the shim
|
|
19
|
+
# terminates the job so orphans are impossible. persist mode (guard-run
|
|
20
|
+
# --service) keeps only the cap; the tree may outlive the launcher.
|
|
21
|
+
|
|
22
|
+
$ErrorActionPreference = "Stop"
|
|
23
|
+
|
|
24
|
+
if ($args.Count -ne 4) {
|
|
25
|
+
[Console]::Error.WriteLine("usage: guard-run-job-object.ps1 <limit> <kill|persist> <launcherPid> <base64CommandLine>")
|
|
26
|
+
exit 2
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
$activeProcessLimit = 0
|
|
30
|
+
if (-not [int]::TryParse($args[0], [ref]$activeProcessLimit) -or $activeProcessLimit -lt 1) {
|
|
31
|
+
[Console]::Error.WriteLine("guard-run-job-object: limit must be a positive integer")
|
|
32
|
+
exit 2
|
|
33
|
+
}
|
|
34
|
+
if ($args[1] -notin @("kill", "persist")) {
|
|
35
|
+
[Console]::Error.WriteLine("guard-run-job-object: mode must be 'kill' or 'persist'")
|
|
36
|
+
exit 2
|
|
37
|
+
}
|
|
38
|
+
$killOnClose = $args[1] -eq "kill"
|
|
39
|
+
$launcherPid = 0
|
|
40
|
+
if (-not [int]::TryParse($args[2], [ref]$launcherPid)) {
|
|
41
|
+
[Console]::Error.WriteLine("guard-run-job-object: launcherPid must be an integer")
|
|
42
|
+
exit 2
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
$commandLine = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($args[3]))
|
|
46
|
+
} catch {
|
|
47
|
+
[Console]::Error.WriteLine("guard-run-job-object: command line is not valid base64")
|
|
48
|
+
exit 2
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
Add-Type -TypeDefinition @'
|
|
52
|
+
using System;
|
|
53
|
+
using System.Runtime.InteropServices;
|
|
54
|
+
using System.Text;
|
|
55
|
+
|
|
56
|
+
namespace MuggleGuardRun
|
|
57
|
+
{
|
|
58
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
59
|
+
public struct JOBOBJECT_BASIC_LIMIT_INFORMATION
|
|
60
|
+
{
|
|
61
|
+
public long PerProcessUserTimeLimit;
|
|
62
|
+
public long PerJobUserTimeLimit;
|
|
63
|
+
public uint LimitFlags;
|
|
64
|
+
public UIntPtr MinimumWorkingSetSize;
|
|
65
|
+
public UIntPtr MaximumWorkingSetSize;
|
|
66
|
+
public uint ActiveProcessLimit;
|
|
67
|
+
public UIntPtr Affinity;
|
|
68
|
+
public uint PriorityClass;
|
|
69
|
+
public uint SchedulingClass;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
73
|
+
public struct IO_COUNTERS
|
|
74
|
+
{
|
|
75
|
+
public ulong ReadOperationCount;
|
|
76
|
+
public ulong WriteOperationCount;
|
|
77
|
+
public ulong OtherOperationCount;
|
|
78
|
+
public ulong ReadTransferCount;
|
|
79
|
+
public ulong WriteTransferCount;
|
|
80
|
+
public ulong OtherTransferCount;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
84
|
+
public struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
|
|
85
|
+
{
|
|
86
|
+
public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation;
|
|
87
|
+
public IO_COUNTERS IoInfo;
|
|
88
|
+
public UIntPtr ProcessMemoryLimit;
|
|
89
|
+
public UIntPtr JobMemoryLimit;
|
|
90
|
+
public UIntPtr PeakProcessMemoryUsed;
|
|
91
|
+
public UIntPtr PeakJobMemoryUsed;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
95
|
+
public struct STARTUPINFO
|
|
96
|
+
{
|
|
97
|
+
public uint cb;
|
|
98
|
+
public string lpReserved;
|
|
99
|
+
public string lpDesktop;
|
|
100
|
+
public string lpTitle;
|
|
101
|
+
public uint dwX;
|
|
102
|
+
public uint dwY;
|
|
103
|
+
public uint dwXSize;
|
|
104
|
+
public uint dwYSize;
|
|
105
|
+
public uint dwXCountChars;
|
|
106
|
+
public uint dwYCountChars;
|
|
107
|
+
public uint dwFillAttribute;
|
|
108
|
+
public uint dwFlags;
|
|
109
|
+
public ushort wShowWindow;
|
|
110
|
+
public ushort cbReserved2;
|
|
111
|
+
public IntPtr lpReserved2;
|
|
112
|
+
public IntPtr hStdInput;
|
|
113
|
+
public IntPtr hStdOutput;
|
|
114
|
+
public IntPtr hStdError;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
118
|
+
public struct PROCESS_INFORMATION
|
|
119
|
+
{
|
|
120
|
+
public IntPtr hProcess;
|
|
121
|
+
public IntPtr hThread;
|
|
122
|
+
public uint dwProcessId;
|
|
123
|
+
public uint dwThreadId;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public static class JobObjectLauncher
|
|
127
|
+
{
|
|
128
|
+
const uint JOB_OBJECT_LIMIT_ACTIVE_PROCESS = 0x00000008;
|
|
129
|
+
const uint JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000;
|
|
130
|
+
const int JobObjectExtendedLimitInformation = 9;
|
|
131
|
+
const uint CREATE_SUSPENDED = 0x00000004;
|
|
132
|
+
const uint SYNCHRONIZE = 0x00100000;
|
|
133
|
+
const uint INFINITE = 0xFFFFFFFF;
|
|
134
|
+
const uint WAIT_OBJECT_0 = 0;
|
|
135
|
+
const int LAUNCHER_DEATH_EXIT_CODE = 137;
|
|
136
|
+
|
|
137
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
138
|
+
static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string lpName);
|
|
139
|
+
|
|
140
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
141
|
+
static extern bool SetInformationJobObject(IntPtr hJob, int jobObjectInfoClass,
|
|
142
|
+
ref JOBOBJECT_EXTENDED_LIMIT_INFORMATION lpJobObjectInfo, uint cbJobObjectInfoLength);
|
|
143
|
+
|
|
144
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
145
|
+
static extern bool AssignProcessToJobObject(IntPtr hJob, IntPtr hProcess);
|
|
146
|
+
|
|
147
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
148
|
+
static extern bool TerminateJobObject(IntPtr hJob, uint uExitCode);
|
|
149
|
+
|
|
150
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
151
|
+
static extern bool CreateProcess(string lpApplicationName, StringBuilder lpCommandLine,
|
|
152
|
+
IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,
|
|
153
|
+
uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
|
|
154
|
+
ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
|
|
155
|
+
|
|
156
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
157
|
+
static extern uint ResumeThread(IntPtr hThread);
|
|
158
|
+
|
|
159
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
160
|
+
static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
|
|
161
|
+
|
|
162
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
163
|
+
static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
|
|
164
|
+
|
|
165
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
166
|
+
static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool bWaitAll,
|
|
167
|
+
uint dwMilliseconds);
|
|
168
|
+
|
|
169
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
170
|
+
static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
|
|
171
|
+
|
|
172
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
173
|
+
static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
|
|
174
|
+
|
|
175
|
+
static Exception LastError(string operation)
|
|
176
|
+
{
|
|
177
|
+
return new Exception(operation + " failed (Win32 error " +
|
|
178
|
+
Marshal.GetLastWin32Error() + ")");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
public static int Run(string commandLine, int activeProcessLimit, bool killOnClose,
|
|
182
|
+
int launcherPid)
|
|
183
|
+
{
|
|
184
|
+
IntPtr job = CreateJobObject(IntPtr.Zero, null);
|
|
185
|
+
if (job == IntPtr.Zero) throw LastError("CreateJobObject");
|
|
186
|
+
|
|
187
|
+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits =
|
|
188
|
+
new JOBOBJECT_EXTENDED_LIMIT_INFORMATION();
|
|
189
|
+
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_ACTIVE_PROCESS
|
|
190
|
+
| (killOnClose ? JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE : 0);
|
|
191
|
+
limits.BasicLimitInformation.ActiveProcessLimit = (uint)activeProcessLimit;
|
|
192
|
+
uint limitsSize = (uint)Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
|
|
193
|
+
if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation, ref limits,
|
|
194
|
+
limitsSize))
|
|
195
|
+
{
|
|
196
|
+
throw LastError("SetInformationJobObject");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
STARTUPINFO startupInfo = new STARTUPINFO();
|
|
200
|
+
startupInfo.cb = (uint)Marshal.SizeOf(typeof(STARTUPINFO));
|
|
201
|
+
PROCESS_INFORMATION processInfo;
|
|
202
|
+
StringBuilder mutableCommandLine = new StringBuilder(commandLine);
|
|
203
|
+
if (!CreateProcess(null, mutableCommandLine, IntPtr.Zero, IntPtr.Zero, true,
|
|
204
|
+
CREATE_SUSPENDED, IntPtr.Zero, null, ref startupInfo, out processInfo))
|
|
205
|
+
{
|
|
206
|
+
throw LastError("CreateProcess for [" + commandLine + "]");
|
|
207
|
+
}
|
|
208
|
+
if (!AssignProcessToJobObject(job, processInfo.hProcess))
|
|
209
|
+
{
|
|
210
|
+
TerminateProcess(processInfo.hProcess, 1);
|
|
211
|
+
throw LastError("AssignProcessToJobObject");
|
|
212
|
+
}
|
|
213
|
+
ResumeThread(processInfo.hThread);
|
|
214
|
+
|
|
215
|
+
if (killOnClose)
|
|
216
|
+
{
|
|
217
|
+
IntPtr launcher = OpenProcess(SYNCHRONIZE, false, (uint)launcherPid);
|
|
218
|
+
if (launcher == IntPtr.Zero)
|
|
219
|
+
{
|
|
220
|
+
// Launcher already gone: the tree is orphaned at birth — reap it.
|
|
221
|
+
TerminateJobObject(job, LAUNCHER_DEATH_EXIT_CODE);
|
|
222
|
+
return LAUNCHER_DEATH_EXIT_CODE;
|
|
223
|
+
}
|
|
224
|
+
IntPtr[] handles = new IntPtr[] { processInfo.hProcess, launcher };
|
|
225
|
+
uint signaled = WaitForMultipleObjects(2, handles, false, INFINITE);
|
|
226
|
+
if (signaled == WAIT_OBJECT_0 + 1)
|
|
227
|
+
{
|
|
228
|
+
TerminateJobObject(job, LAUNCHER_DEATH_EXIT_CODE);
|
|
229
|
+
return LAUNCHER_DEATH_EXIT_CODE;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
else
|
|
233
|
+
{
|
|
234
|
+
WaitForSingleObject(processInfo.hProcess, INFINITE);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
uint exitCode;
|
|
238
|
+
if (!GetExitCodeProcess(processInfo.hProcess, out exitCode))
|
|
239
|
+
{
|
|
240
|
+
throw LastError("GetExitCodeProcess");
|
|
241
|
+
}
|
|
242
|
+
// The job handle is deliberately never closed here: in kill mode this
|
|
243
|
+
// process's death is what closes it, and that close is the kernel's
|
|
244
|
+
// cue to destroy any stragglers.
|
|
245
|
+
return (int)exitCode;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
'@
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
exit ([MuggleGuardRun.JobObjectLauncher]::Run($commandLine, $activeProcessLimit, $killOnClose, $launcherPid))
|
|
253
|
+
} catch {
|
|
254
|
+
[Console]::Error.WriteLine("guard-run-job-object: $($_.Exception.Message)")
|
|
255
|
+
exit 1
|
|
256
|
+
}
|