Repository navigation
Expand file tree
/
Copy pathclearPrintQueue.ts
More file actions
194 lines (169 loc) · 5.99 KB
/
Copy pathclearPrintQueue.ts
File metadata and controls
194 lines (169 loc) · 5.99 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
/**
* mcp/skills/clearPrintQueue.ts — clear_print_queue skill
*
* Cancels all pending and stuck print jobs. Use when the print queue is
* jammed and jobs cannot be removed through normal means.
*
* Platform strategy
* -----------------
* darwin `cancel -a` — cancels all jobs across all queues
* win32 PowerShell Get-PrintJob | Remove-PrintJob
*
* Smoke test
* npx tsx -r dotenv/config mcp/skills/clearPrintQueue.ts
*/
import * as os from "os";
import { exec } from "child_process";
import { promisify } from "util";
import { z } from "zod";
const execAsync = promisify(exec);
// -- Meta ---------------------------------------------------------------------
export const meta = {
name: "clear_print_queue",
description:
"Cancels all pending and stuck print jobs. " +
"Use when the print queue is jammed and jobs cannot be removed through normal means.",
riskLevel: "medium",
// Cancelling jobs is irreversible (queued work is lost), so destructive:true.
// This is also what makes G4 auto-fire the dry-run preview + consent flow
// (autoTriggerDryRun = supportsDryRun && (riskLevel>=high || destructive)).
// Without it, the tool's dryRun:true default wins and the cancel never runs.
destructive: true,
requiresConsent: true,
supportsDryRun: true,
affectedScope: ["system"],
auditRequired: true,
escalationHint: {
darwin: "sudo cancel -a # clears all queues for all users",
win32: "Stop-Service Spooler -Force; Remove-Item C:\\Windows\\System32\\spool\\PRINTERS\\* -Force; Start-Service Spooler # run from elevated PowerShell",
},
outputKeys: ["cancelledCount","printers","jobs","dryRun","message"],
schema: {
// No per-printer targeting: this tool always clears ALL queues. The
// privileged helper daemon's clear_print_queue accepts exactly `{}`
// (deny_unknown_fields), and the local dry-run preview must match the
// helper's clear-all real run, so a `printerName` param would (a) be
// rejected by the helper and (b) make the preview disagree with the action.
dryRun: z
.boolean()
.nullable().optional()
.describe(
"If true, show jobs that would be cancelled without cancelling. Default: true",
),
},
} as const;
// -- Types --------------------------------------------------------------------
interface ClearPrintQueueResult {
cancelledCount: number;
printers: string[];
jobs: string[];
dryRun: boolean;
message: string;
}
// -- PowerShell helper --------------------------------------------------------
async function runPS(script: string): Promise<string> {
const encoded = Buffer.from(script, "utf16le").toString("base64");
const { stdout } = await execAsync(
`powershell.exe -NoProfile -NonInteractive -EncodedCommand ${encoded}`,
{ maxBuffer: 20 * 1024 * 1024 },
);
return stdout.trim();
}
// -- darwin implementation ----------------------------------------------------
async function clearPrintQueueDarwin(
dryRun: boolean,
): Promise<ClearPrintQueueResult> {
// Enumerate jobs across all queues.
let lpstatOut = "";
try {
const { stdout } = await execAsync("lpstat -o");
lpstatOut = stdout;
} catch (err) {
lpstatOut = (err as { stdout?: string }).stdout ?? "";
}
const jobLines = lpstatOut.trim().split("\n").filter(Boolean);
const jobs = jobLines.map((l) => l.trim().split(/\s+/)[0] ?? "").filter(Boolean);
const printers = [...new Set(
jobs.map((j) => j.replace(/-\d+$/, "")).filter(Boolean),
)];
if (!dryRun) {
try {
await execAsync("cancel -a"); // all queues, all users
} catch {
// Non-fatal — queue may already be empty
}
}
const message = dryRun
? `Found ${jobs.length} job(s) in queue. Run with dryRun=false to cancel them.`
: `Cancelled ${jobs.length} job(s) from the print queue.`;
return {
cancelledCount: dryRun ? 0 : jobs.length,
printers,
jobs,
dryRun,
message,
};
}
// -- win32 implementation -----------------------------------------------------
async function clearPrintQueueWin32(
dryRun: boolean,
): Promise<ClearPrintQueueResult> {
// Enumerate jobs across all printers.
const listPs = `
$ErrorActionPreference = 'SilentlyContinue'
$jobs = Get-Printer | ForEach-Object {
$n = $_.Name
Get-PrintJob -PrinterName $n -ErrorAction SilentlyContinue |
Select-Object Id,DocumentName,@{Name='PrinterName';Expression={$n}}
}
$jobs | ConvertTo-Json -Depth 2 -Compress`.trim();
const raw = await runPS(listPs);
let jobItems: any[] = [];
if (raw) {
try {
const parsed = JSON.parse(raw);
jobItems = Array.isArray(parsed) ? parsed : [parsed];
} catch { /* ignore parse errors */ }
}
const jobs = jobItems.map((j) => String(j.DocumentName ?? j.Id ?? "")).filter(Boolean);
const printers = [...new Set(
jobItems.map((j) => String(j.PrinterName ?? "")).filter(Boolean),
)];
if (!dryRun && jobItems.length > 0) {
try {
const cancelPs = `
$ErrorActionPreference = 'SilentlyContinue'
Get-Printer | ForEach-Object {
Get-PrintJob -PrinterName $_.Name -ErrorAction SilentlyContinue | Remove-PrintJob
}`.trim();
await runPS(cancelPs);
} catch { /* Non-fatal */ }
}
const message = dryRun
? `Found ${jobs.length} job(s) in queue. Run with dryRun=false to cancel them.`
: `Cancelled ${jobs.length} job(s) from the print queue.`;
return {
cancelledCount: dryRun ? 0 : jobs.length,
printers,
jobs,
dryRun,
message,
};
}
// -- Exported run function ----------------------------------------------------
export async function run({
dryRun = true,
}: {
dryRun?: boolean;
} = {}) {
const platform = os.platform();
return platform === "win32"
? clearPrintQueueWin32(dryRun)
: clearPrintQueueDarwin(dryRun);
}
// -- Smoke test ---------------------------------------------------------------
if (false) {
run({})
.then(r => console.log(JSON.stringify(r, null, 2)))
.catch((err: Error) => { console.error(err.message); process.exit(1); });
}