@crewhaus/tool-pkgmgr 0.7.0 → 0.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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,9 @@
1
+ CrewHaus Factory
2
+ Copyright 2026 Max Meier
3
+
4
+ This product includes software developed by Max Meier
5
+ (operating as StudioMax — https://studiomax.io) and contributors.
6
+
7
+ The CrewHaus name and logo are trademarks of Max Meier. See
8
+ https://github.com/crewhaus/factory/blob/main/TRADEMARK.md for the
9
+ use-of-marks policy.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @crewhaus/tool-pkgmgr
2
+
3
+ Deterministic system package-manager queries and installs, with no privilege escalation and a dry run that runs the real selection
4
+
5
+ Part of [CrewHaus](https://github.com/crewhaus/factory). Most people install the CLI (`npm install -g crewhaus`) rather than this package.
6
+
7
+ Which tools a spec can turn on, and how: [tools reference](https://github.com/crewhaus/docs/blob/main/TOOLS-REFERENCE.md).
8
+
9
+ Requires [Bun](https://bun.sh) `>=1.3.14`; plain Node is not supported.
10
+
11
+ [Source](https://github.com/crewhaus/factory/tree/main/packages/tool-pkgmgr)
package/dist/index.js CHANGED
@@ -121,7 +121,8 @@ function factsJson(facts) {
121
121
  // ---------------------------------------------------------------------------
122
122
  export const packageQuery = buildTool({
123
123
  name: "PackageQuery",
124
- description: 'Ask the system package manager whether a package is installed, at what version, and what its already-downloaded index says is available. Supports Homebrew, apt/dpkg, dnf/rpm, pacman, winget and chocolatey, detecting the manager from the platform unless you name one; a host with none of them is reported as unknown rather than guessed at. It makes NO network call: every answer comes from a local index, which is why winget and chocolatey report installed state only — their available version lives on a remote source. Nothing is ever defaulted: a manager that is not on this host, a probe that timed out, an empty dnf metadata cache or a winget row whose columns could not be split all come back as status "unknown" with the reason and the exact command that was run, never as "not installed". Read-only: it runs only query commands and changes nothing.',
124
+ operativeArgs: [{ field: "name", kind: "id" }],
125
+ description: 'Ask the system package manager whether a package is installed, at what version, and what its already-downloaded index says is available. Supports Homebrew, apt/dpkg, dnf/rpm, pacman, winget and chocolatey, detecting the manager from the platform unless you name one; a host with none of them is reported as unknown rather than guessed at. It asks for nothing over the network: every answer comes from a local index, which is why winget and chocolatey report installed state only — their available version lives on a remote source (winget may still refresh its own source cache when a list opens it; no source agreement is ever accepted for you). Nothing is ever defaulted: a manager that is not on this host, a probe that timed out, an empty dnf metadata cache or a winget row whose columns could not be split all come back as status "unknown" with the reason and the exact command that was run, never as "not installed". Read-only: it runs only query commands and changes nothing.',
125
126
  inputSchema: z.object({
126
127
  name: nameSchema,
127
128
  manager: managerSchema,
@@ -209,12 +210,16 @@ function refusal(fields) {
209
210
  }
210
211
  : {}),
211
212
  ...(fields.plan !== undefined ? { plan: fields.plan } : {}),
213
+ ...(fields.unknowns !== undefined && fields.unknowns.size > 0
214
+ ? { unknown: fields.unknowns.list() }
215
+ : {}),
212
216
  probes: probeJson(fields.probes),
213
217
  });
214
218
  }
215
219
  export const packageInstall = buildTool({
216
220
  name: "PackageInstall",
217
- description: "Install a package through the system package manager, or report exactly what installing it would do. DESTRUCTIVE: an install can replace a version that is already working, so the plan names the versions currently on disk before anything runs. Pass dryRun to get that plan and install nothing — it is resolved by the same code the real install uses, and includes the transitive packages the manager says it would add (complete from apt and pacman, resolved-then-declined from dnf, derived from `brew deps` for Homebrew, which has no dry run). THIS TOOL NEVER ACQUIRES PRIVILEGE. It runs no sudo, doas, runas or pkexec and raises no UAC prompt. Homebrew needs no root and is the case that genuinely works unattended; apt, dnf and pacman need root, so they work only when this process is ALREADY root and are otherwise refused with the exact command an operator would run themselves; winget and chocolatey installs are refused outright, because both end in an elevation prompt. A version can only be pinned where the manager can express one — apt can, and every other manager here is refused with the reason rather than quietly installing latest. A package name beginning with '-' is refused, because a manager would read it as a flag.",
221
+ operativeArgs: [{ field: "name", kind: "id" }],
222
+ description: "Install a package through the system package manager, or report what installing it would do. DESTRUCTIVE: an install can replace a working version, so the plan names the versions on disk first. dryRun returns that plan and installs nothing; it runs the same code as the real install and lists the packages the manager would add. It never acquires privilege: no sudo, doas, runas, pkexec or UAC prompt. Homebrew needs no root; apt, dnf and pacman work only when this process is already root, and otherwise return the command to run yourself; winget and chocolatey installs are refused. Only apt can pin a version; the other managers refuse one rather than install latest. It never removes anything: a plan that would remove a package is refused, and apt must know the exact name (and version) asked for. A name beginning with '-' is refused.",
218
223
  inputSchema: z.object({
219
224
  name: nameSchema,
220
225
  manager: managerSchema,
@@ -226,7 +231,7 @@ export const packageInstall = buildTool({
226
231
  dryRun: z
227
232
  .boolean()
228
233
  .optional()
229
- .describe("resolve and report the plan, and install nothing (default false). The plan is produced by the same code the real install runs"),
234
+ .describe("resolve and report the plan, and install nothing (default false). The plan is produced by the same code the real install runs; its added packages are complete from apt and pacman, resolved-then-declined from dnf, and derived from `brew deps` for Homebrew, which has no dry run"),
230
235
  timeoutMs: timeoutSchema,
231
236
  }),
232
237
  destructive: true,
@@ -293,8 +298,35 @@ export const packageInstall = buildTool({
293
298
  const operand = buildOperand(manager, input.name, requestedVersion ?? undefined);
294
299
  // ---- resolution: the same code for both paths -------------------------
295
300
  const facts = await queryPackage(ctx, manager, input.name, unknowns, true);
301
+ // apt reads an operand it has no exact package for as a pattern, and a
302
+ // version it does not list is stripped to one it does. Nothing is planned
303
+ // or installed until apt has answered about exactly this name (C021).
304
+ if (manager === "apt") {
305
+ const inexact = aptExactRefusal(input.name, requestedVersion, facts);
306
+ if (inexact !== undefined) {
307
+ return refusal({
308
+ platform,
309
+ manager,
310
+ reason: inexact,
311
+ probes: ctx.probes,
312
+ dryRun,
313
+ unknowns,
314
+ });
315
+ }
316
+ }
296
317
  const plan = await resolveInstallPlan(ctx, manager, input.name, operand, requestedVersion, facts, unknowns);
318
+ const removals = removalRefusal(plan);
297
319
  if (dryRun) {
320
+ // Stated on the dry run too, so a caller learns BEFORE it tries that
321
+ // the real call would be refused on this host, or for this plan.
322
+ const wouldRefuse = [
323
+ ...(spec.install.needsRoot && !isRoot()
324
+ ? [
325
+ `${spec.label} needs root to install and this process is not root; this tool does not acquire privilege`,
326
+ ]
327
+ : []),
328
+ ...(removals !== undefined ? [removals] : []),
329
+ ];
298
330
  return json({
299
331
  tool: "PackageInstall",
300
332
  platform,
@@ -304,12 +336,10 @@ export const packageInstall = buildTool({
304
336
  installed: false,
305
337
  plan,
306
338
  before: factsJson(facts),
307
- // Stated on the dry run too, so a caller learns BEFORE it tries that
308
- // the real call would be refused on this host.
309
- ...(spec.install.needsRoot && !isRoot()
339
+ ...(wouldRefuse.length > 0
310
340
  ? {
311
341
  wouldBeRefused: true,
312
- wouldBeRefusedReason: `${spec.label} needs root to install and this process is not root; this tool does not acquire privilege`,
342
+ wouldBeRefusedReason: wouldRefuse.join("; "),
313
343
  operatorCommand: spec.operatorCommand(operand),
314
344
  }
315
345
  : {}),
@@ -344,6 +374,17 @@ export const packageInstall = buildTool({
344
374
  probes: probeJson(ctx.probes),
345
375
  });
346
376
  }
377
+ if (removals !== undefined) {
378
+ return refusal({
379
+ platform,
380
+ manager,
381
+ reason: removals,
382
+ operatorCommand: spec.operatorCommand(operand),
383
+ plan,
384
+ probes: ctx.probes,
385
+ dryRun: false,
386
+ });
387
+ }
347
388
  // ---- the one line that differs from the dry run ------------------------
348
389
  const installArgv = installCommand(manager, operand);
349
390
  const result = await run(ctx, manager, installArgv);
@@ -402,6 +443,37 @@ export const packageInstall = buildTool({
402
443
  });
403
444
  },
404
445
  });
446
+ /**
447
+ * Why apt's answer does not license an install of exactly `name` (at
448
+ * `version`), or undefined when it does. `facts.versionTable` exists only
449
+ * when `apt-cache policy` answered with one stanza headed by this name.
450
+ */
451
+ function aptExactRefusal(name, version, facts) {
452
+ const table = facts.versionTable;
453
+ if (table === undefined) {
454
+ return facts.knownToManager === false
455
+ ? `apt has no package named exactly ${JSON.stringify(name)} in its downloaded lists, so there is nothing to install`
456
+ : `apt did not answer about a package named exactly ${JSON.stringify(name)} — it read the name as a pattern, or its answer could not be read (see unknown) — and an install would act on whatever apt matched instead`;
457
+ }
458
+ if (version !== null && !table.includes(version)) {
459
+ const known = table.length === 0 ? "none" : table.slice(0, 5).join(", ");
460
+ return `apt's lists have no version ${JSON.stringify(version)} of ${JSON.stringify(name)} (listed: ${known}); apt would install a different version or fail, so nothing was run`;
461
+ }
462
+ return undefined;
463
+ }
464
+ /**
465
+ * The refusal for a plan whose simulation REMOVES something. This tool
466
+ * installs; an install that removes a package (a Conflicts swap, or an
467
+ * operand apt read as "remove") is a different decision, and the real
468
+ * install also runs with `--no-remove`, so apt would abort it anyway.
469
+ */
470
+ function removalRefusal(plan) {
471
+ const removed = plan.removals ?? [];
472
+ if (removed.length === 0)
473
+ return undefined;
474
+ const names = removed.map((p) => p.name).join(", ");
475
+ return `the manager's simulation of this install REMOVES ${names}; this tool installs and never removes, so it refuses — run the operator command yourself if the removal is intended`;
476
+ }
405
477
  /**
406
478
  * The install argv for a manager.
407
479
  *
@@ -61,10 +61,14 @@ export declare const MANAGER_IDS: readonly ManagerId[];
61
61
  * - `pacman -S --print --print-format '%r/%n %v'` is pacman's dry run. It
62
62
  * prints every target INCLUDING dependencies, with versions, and needs no
63
63
  * root — the best dry run of the six.
64
- * - `winget list --exact --query` with `--disable-interactivity` and
65
- * `--accept-source-agreements`: without those two, winget can sit waiting
66
- * for a keypress or for an agreement to be accepted, which on a pipe is a
67
- * command that never exits.
64
+ * - `winget list --exact --query` with `--disable-interactivity`, which is
65
+ * what stops winget waiting on a pipe for a keypress: with it, a source
66
+ * agreement nobody has accepted makes winget REFUSE (exit 0x8A150046)
67
+ * instead of prompting. `--accept-source-agreements` is deliberately
68
+ * absent. winget records that acceptance permanently — for msstore it is
69
+ * consent to send the machine's region — and accepting a licence is the
70
+ * operator's decision, never a side effect of a read. The refusal is
71
+ * reported as unknown, with the command to run once by hand.
68
72
  * - `choco list --limit-output` is chocolatey's documented machine-readable
69
73
  * mode: `name|version`, one per line, no headers, no colour. The v1/v2
70
74
  * split is real — `--local-only` was removed in Chocolatey 2.0 — so the
@@ -34,10 +34,14 @@ export const MANAGER_IDS = Object.freeze([
34
34
  * - `pacman -S --print --print-format '%r/%n %v'` is pacman's dry run. It
35
35
  * prints every target INCLUDING dependencies, with versions, and needs no
36
36
  * root — the best dry run of the six.
37
- * - `winget list --exact --query` with `--disable-interactivity` and
38
- * `--accept-source-agreements`: without those two, winget can sit waiting
39
- * for a keypress or for an agreement to be accepted, which on a pipe is a
40
- * command that never exits.
37
+ * - `winget list --exact --query` with `--disable-interactivity`, which is
38
+ * what stops winget waiting on a pipe for a keypress: with it, a source
39
+ * agreement nobody has accepted makes winget REFUSE (exit 0x8A150046)
40
+ * instead of prompting. `--accept-source-agreements` is deliberately
41
+ * absent. winget records that acceptance permanently — for msstore it is
42
+ * consent to send the machine's region — and accepting a licence is the
43
+ * operator's decision, never a side effect of a read. The refusal is
44
+ * reported as unknown, with the command to run once by hand.
41
45
  * - `choco list --limit-output` is chocolatey's documented machine-readable
42
46
  * mode: `name|version`, one per line, no headers, no colour. The v1/v2
43
47
  * split is real — `--local-only` was removed in Chocolatey 2.0 — so the
@@ -56,9 +60,31 @@ export const PKGMGR_COMMANDS = Object.freeze({
56
60
  "-f=${binary:Package}\t${Version}\t${db:Status-Abbrev}\n",
57
61
  "--",
58
62
  ]),
59
- aptCachePolicy: Object.freeze(["apt-cache", "policy", "--"]),
60
- aptSimulate: Object.freeze(["apt-get", "install", "-s", "-y", "--"]),
61
- aptInstall: Object.freeze(["apt-get", "install", "-y", "--"]),
63
+ // `-o APT::Cmd::Pattern-Only=true`: apt reads an operand as a package name
64
+ // or an explicit `?pattern`, never as the unanchored regex or glob it falls
65
+ // back to otherwise (`lib.+` matched 116 packages on Debian 12). The name
66
+ // gate already refuses pattern syntax; this is the belt. Older apt ignores
67
+ // a configuration key it does not know. `--no-remove` on the real install:
68
+ // apt aborts rather than remove anything, whatever the operand says.
69
+ aptCachePolicy: Object.freeze(["apt-cache", "policy", "-o", "APT::Cmd::Pattern-Only=true", "--"]),
70
+ aptSimulate: Object.freeze([
71
+ "apt-get",
72
+ "install",
73
+ "-s",
74
+ "-y",
75
+ "-o",
76
+ "APT::Cmd::Pattern-Only=true",
77
+ "--",
78
+ ]),
79
+ aptInstall: Object.freeze([
80
+ "apt-get",
81
+ "install",
82
+ "-y",
83
+ "--no-remove",
84
+ "-o",
85
+ "APT::Cmd::Pattern-Only=true",
86
+ "--",
87
+ ]),
62
88
  dnfVersion: Object.freeze(["dnf", "--version"]),
63
89
  rpmQuery: Object.freeze(["rpm", "-q", "--queryformat", "%{NAME}\t%{EVR}\t%{ARCH}\n", "--"]),
64
90
  dnfListAvailable: Object.freeze(["dnf", "--cacheonly", "--quiet", "list", "--available", "--"]),
@@ -70,14 +96,7 @@ export const PKGMGR_COMMANDS = Object.freeze({
70
96
  pacmanSimulate: Object.freeze(["pacman", "-S", "--print", "--print-format", "%r/%n %v", "--"]),
71
97
  pacmanInstall: Object.freeze(["pacman", "-S", "--noconfirm", "--"]),
72
98
  wingetVersion: Object.freeze(["winget", "--version"]),
73
- wingetList: Object.freeze([
74
- "winget",
75
- "list",
76
- "--exact",
77
- "--disable-interactivity",
78
- "--accept-source-agreements",
79
- "--query",
80
- ]),
99
+ wingetList: Object.freeze(["winget", "list", "--exact", "--disable-interactivity", "--query"]),
81
100
  chocoVersion: Object.freeze(["choco", "--version"]),
82
101
  /** Chocolatey 2.x: `choco list` is local by default. */
83
102
  chocoListV2: Object.freeze(["choco", "list", "--exact", "--limit-output"]),
@@ -47,7 +47,11 @@ export type NameRule = {
47
47
  * tap-qualified name adds two slashes (`homebrew/cask/firefox`). Upper
48
48
  * case is allowed through because a tap's user segment can carry it.
49
49
  * - apt: Debian policy §5.6.1 — `[a-z0-9][a-z0-9+.-]+`, plus an optional
50
- * `:arch` qualifier (`libc6:i386`).
50
+ * `:arch` qualifier (`libc6:i386`). Neither part may END in `-`: apt-get
51
+ * reads a trailing `-` on an install operand as "remove this package"
52
+ * (`apt-get install tzdata-` uninstalls tzdata), and no real package or
53
+ * architecture name ends in one. A trailing `+` IS legal (`g++`), and apt
54
+ * matches an exact name before it reads `+` as a modifier.
51
55
  * - dnf: an rpm name is `[A-Za-z0-9._+-]`, and the whole NEVRA form
52
56
  * (`kernel-5.14.0-427.el9.x86_64`) is spelled with the same characters,
53
57
  * which is why a version is passed as part of the NAME for dnf rather
package/dist/lib/names.js CHANGED
@@ -40,7 +40,11 @@
40
40
  * tap-qualified name adds two slashes (`homebrew/cask/firefox`). Upper
41
41
  * case is allowed through because a tap's user segment can carry it.
42
42
  * - apt: Debian policy §5.6.1 — `[a-z0-9][a-z0-9+.-]+`, plus an optional
43
- * `:arch` qualifier (`libc6:i386`).
43
+ * `:arch` qualifier (`libc6:i386`). Neither part may END in `-`: apt-get
44
+ * reads a trailing `-` on an install operand as "remove this package"
45
+ * (`apt-get install tzdata-` uninstalls tzdata), and no real package or
46
+ * architecture name ends in one. A trailing `+` IS legal (`g++`), and apt
47
+ * matches an exact name before it reads `+` as a modifier.
44
48
  * - dnf: an rpm name is `[A-Za-z0-9._+-]`, and the whole NEVRA form
45
49
  * (`kernel-5.14.0-427.el9.x86_64`) is spelled with the same characters,
46
50
  * which is why a version is passed as part of the NAME for dnf rather
@@ -62,8 +66,8 @@ export const NAME_RULES = Object.freeze({
62
66
  charset: "letters, digits and @ + . _ - , with at most two / segments for a tap-qualified name",
63
67
  },
64
68
  apt: {
65
- pattern: /^[a-z0-9][a-z0-9+._-]*(:[a-z0-9][a-z0-9-]*)?$/,
66
- charset: "lowercase letters, digits and + . _ - , with an optional :architecture suffix",
69
+ pattern: /^[a-z0-9](?:[a-z0-9+._-]*[a-z0-9+._])?(?::[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)?$/,
70
+ charset: "lowercase letters, digits and + . _ - (not ending in -), with an optional :architecture suffix",
67
71
  },
68
72
  dnf: {
69
73
  pattern: /^[A-Za-z0-9][A-Za-z0-9._+-]*$/,
@@ -129,6 +133,12 @@ export function checkPackageName(manager, name) {
129
133
  if (/\s/.test(name)) {
130
134
  return "the package name contains whitespace; a package name is a single token";
131
135
  }
136
+ if (manager === "apt" && name.split(":").some((part) => part.endsWith("-"))) {
137
+ // Named before the charset check, because the reason is not the
138
+ // characters: apt-get reads `install tzdata-` as REMOVE tzdata, so an
139
+ // accepted name would turn an install call into an uninstall.
140
+ return `${JSON.stringify(name)} ends in "-", which apt-get reads as "remove this package", not as part of its name — no apt package or architecture name ends in "-"`;
141
+ }
132
142
  const rule = NAME_RULES[manager];
133
143
  if (!rule.pattern.test(name)) {
134
144
  return `${JSON.stringify(name)} is not a valid ${manager} package name — accepted: ${rule.charset}`;
@@ -170,5 +180,11 @@ export function checkVersionSyntax(version) {
170
180
  if (!DEBIAN_VERSION_PATTERN.test(version)) {
171
181
  return `${JSON.stringify(version)} is not a Debian version string — accepted: letters, digits and . + ~ : - , starting with a letter or digit`;
172
182
  }
183
+ if (version.endsWith("-")) {
184
+ // A Debian revision is never empty, so no real version ends in "-"; and
185
+ // apt-get reads `name=version-` as "remove name" (verified on apt 2.6:
186
+ // `install tzdata=2026b-0+deb12u1-` uninstalled tzdata).
187
+ return `${JSON.stringify(version)} ends in "-", which is never a Debian version and which apt-get reads as "remove this package"`;
188
+ }
173
189
  return undefined;
174
190
  }
@@ -100,6 +100,49 @@ export type AptPolicy = {
100
100
  readonly installed: string | null;
101
101
  readonly candidate: string | null;
102
102
  };
103
+ /** One `<name>:` stanza of `apt-cache policy` output. */
104
+ export type AptPolicyStanza = AptPolicy & {
105
+ /** The header without its trailing colon: `curl`, `libc6:i386`. */
106
+ readonly header: string;
107
+ /** Every version the version table lists, in apt's order. */
108
+ readonly versions: readonly string[];
109
+ /** Whether the Installed or Candidate label was found at all. */
110
+ readonly labelled: boolean;
111
+ };
112
+ /**
113
+ * Split `apt-cache policy` output into its stanzas. A stanza starts at an
114
+ * unindented `<name>:` line. apt prints one per package the operand matched,
115
+ * and an operand it read as a pattern matches many: `apt-cache policy
116
+ * 'lib.+'` printed 116 on a Debian 12 image.
117
+ */
118
+ export declare function parseAptPolicyStanzas(stdout: string): AptPolicyStanza[];
119
+ /** What `apt-cache policy <name>` said about exactly `name`. */
120
+ export type AptPolicyMatch =
121
+ /** One stanza, headed by exactly this name. */
122
+ {
123
+ readonly kind: "exact";
124
+ readonly stanza: AptPolicyStanza;
125
+ }
126
+ /** Nothing at all: apt has no package of this name (it prints nothing). */
127
+ | {
128
+ readonly kind: "none";
129
+ }
130
+ /** Stanzas, but not one headed by exactly this name: apt read it as a pattern. */
131
+ | {
132
+ readonly kind: "not-exact";
133
+ readonly headers: readonly string[];
134
+ }
135
+ /** A stanza for this name, but neither label: localised or changed output. */
136
+ | {
137
+ readonly kind: "unlabelled";
138
+ };
139
+ /**
140
+ * The stanza for exactly `name`, or why there is none. `name:arch` also
141
+ * matches a lone stanza headed by the bare name, which is how apt prints the
142
+ * native architecture (`libc6:amd64` on amd64 prints `libc6:`). More than one
143
+ * stanza is never an answer about `name`, whatever their headers say.
144
+ */
145
+ export declare function matchAptPolicy(stdout: string, name: string): AptPolicyMatch;
103
146
  /**
104
147
  * `apt-cache policy <name>` under `LC_ALL=C`.
105
148
  *
@@ -115,8 +158,11 @@ export type AptPolicy = {
115
158
  * An unknown package produces an empty document (apt-cache prints nothing and
116
159
  * exits 0), which parses to `undefined` rather than to two nulls, so "apt has
117
160
  * never heard of this" cannot be confused with "apt has it, not installed".
161
+ * So does output with more than one stanza, or (given `name`) a stanza for a
162
+ * different name: that is apt answering about a PATTERN, and reading its last
163
+ * stanza as the answer reported another package's version (C021).
118
164
  */
119
- export declare function parseAptCachePolicy(stdout: string): AptPolicy | undefined;
165
+ export declare function parseAptCachePolicy(stdout: string, name?: string): AptPolicy | undefined;
120
166
  export type AptSimulatedChange = {
121
167
  readonly action: "install" | "remove";
122
168
  readonly name: string;
@@ -279,14 +325,24 @@ export type WingetRow = {
279
325
  */
280
326
  export declare function parseWingetList(stdout: string): WingetRow[];
281
327
  /**
282
- * winget's "no installed package matched" exit code, 0x8A15002B as a signed
328
+ * winget's "no installed package matched" exit code:
329
+ * APPINSTALLER_CLI_ERROR_NO_APPLICATIONS_FOUND, 0x8A150014, as a signed
283
330
  * 32-bit integer.
284
331
  *
285
332
  * `winget list` exits with this rather than 0-and-empty, which makes presence
286
333
  * decidable from the EXIT CODE alone — the one part of winget's answer that
287
- * is neither localised nor column-aligned.
334
+ * is neither localised nor column-aligned. 0.7.0 had 0x8A15002B here, which
335
+ * is APPINSTALLER_CLI_ERROR_UPDATE_NOT_APPLICABLE (`winget upgrade` with
336
+ * nothing to do), so a package that was not installed came back unknown.
337
+ * Compare against `code | 0`: a runtime may report the DWORD unsigned.
338
+ */
339
+ export declare const WINGET_NO_PACKAGES_FOUND = -1978335212;
340
+ /**
341
+ * APPINSTALLER_CLI_ERROR_SOURCE_AGREEMENTS_NOT_ACCEPTED, 0x8A150046: with
342
+ * `--disable-interactivity`, a configured source whose agreement nobody has
343
+ * accepted makes `winget list` refuse outright instead of prompting.
288
344
  */
289
- export declare const WINGET_NO_PACKAGES_FOUND = -1978335189;
345
+ export declare const WINGET_SOURCE_AGREEMENTS_NOT_ACCEPTED = -1978335162;
290
346
  /**
291
347
  * `choco list --limit-output` — `name|version`, one per line, no header.
292
348
  *