@zhin.js/command 1.1.7 → 1.1.8
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/README.md +54 -1
- package/lib/command-index.d.ts +16 -2
- package/lib/command-index.js +74 -27
- package/lib/command-menu.d.ts +31 -0
- package/lib/command-menu.js +81 -0
- package/lib/definition.d.ts +9 -1
- package/lib/definition.js +12 -5
- package/lib/execution-policy.d.ts +18 -0
- package/lib/execution-policy.js +27 -0
- package/lib/execution-runtime.d.ts +50 -0
- package/lib/execution-runtime.js +134 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/permission-explanation.d.ts +22 -0
- package/lib/permission-explanation.js +54 -0
- package/package.json +8 -4
- package/src/command-index.ts +92 -33
- package/src/command-menu.ts +112 -0
- package/src/definition.ts +17 -6
- package/src/execution-policy.ts +39 -0
- package/src/execution-runtime.ts +165 -0
- package/src/index.ts +1 -0
- package/src/permission-explanation.ts +58 -0
package/src/definition.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { PluginId, RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
|
6
6
|
import {
|
|
7
7
|
createCapabilityContext,
|
|
8
8
|
readOperationClient,
|
|
9
|
+
readOperationInputSignal,
|
|
9
10
|
type AdapterClient,
|
|
10
11
|
type RegisteredAdapterName,
|
|
11
12
|
type CapabilityContext,
|
|
@@ -13,6 +14,8 @@ import {
|
|
|
13
14
|
import { assertPermitSyntax } from '@zhin.js/permission';
|
|
14
15
|
import { validateArgumentSchemas } from './arguments.js';
|
|
15
16
|
import type { UserInteraction } from '@zhin.js/interaction';
|
|
17
|
+
import { normalizeExecutionPolicy, type CommandExecutionPolicy } from './execution-policy.js';
|
|
18
|
+
export type { CommandExecutionPolicy, CommandExecutionLimit } from './execution-policy.js';
|
|
16
19
|
|
|
17
20
|
const commandBrand = 'zhin.command/1' as const;
|
|
18
21
|
|
|
@@ -309,6 +312,8 @@ export interface CommandDefinition<
|
|
|
309
312
|
* 例:`adapter(icqq)`、`role(master)`、`group(123,456)`。
|
|
310
313
|
*/
|
|
311
314
|
readonly permit?: readonly string[];
|
|
315
|
+
/** Optional full-operation cooldown, concurrency and one-shot progress feedback. */
|
|
316
|
+
readonly execution?: Readonly<CommandExecutionPolicy>;
|
|
312
317
|
/**
|
|
313
318
|
* 全局整句快捷方式:触发串(trim 后全文相等)→ 预填 params。
|
|
314
319
|
* 与普通路由一样是全局用户输入,冲突会在 generation 构建时拒绝。
|
|
@@ -383,7 +388,8 @@ export function defineCommand<
|
|
|
383
388
|
validateCommandAlias(definition.alias);
|
|
384
389
|
validateCommandPermit(definition.permit);
|
|
385
390
|
validateCommandShortcutShape(definition.shortcut);
|
|
386
|
-
|
|
391
|
+
const execution = normalizeExecutionPolicy(definition.execution);
|
|
392
|
+
return Object.freeze({ $feature: commandBrand, ...definition, ...(execution ? { execution } : {}) }) as Readonly<
|
|
387
393
|
CommandDefinition<TConfig, TResult, TInput, string | undefined>
|
|
388
394
|
>;
|
|
389
395
|
}
|
|
@@ -464,6 +470,7 @@ export function parseCommandDefinition(value: unknown): CommandDefinition {
|
|
|
464
470
|
throw new TypeError('Command module must default-export defineCommand(...)');
|
|
465
471
|
}
|
|
466
472
|
validateAdapterName(definition.adapter);
|
|
473
|
+
normalizeExecutionPolicy(definition.execution);
|
|
467
474
|
return definition as CommandDefinition;
|
|
468
475
|
}
|
|
469
476
|
|
|
@@ -496,7 +503,7 @@ export function createCommandContext(
|
|
|
496
503
|
adapter?: string,
|
|
497
504
|
options: Readonly<Record<string, CommandParameterValue>> = Object.freeze({}),
|
|
498
505
|
): CommandContext {
|
|
499
|
-
const context = createCapabilityContext(snapshot, ownerId);
|
|
506
|
+
const context = createCapabilityContext(snapshot, ownerId, undefined, readOperationInputSignal(input));
|
|
500
507
|
const session = resolveCommandSession(input);
|
|
501
508
|
const parameterValues: Record<string, CommandParameterValue> = { __proto__: null, ...params };
|
|
502
509
|
const optionValues: Record<string, CommandParameterValue> = { __proto__: null, ...options };
|
|
@@ -534,7 +541,7 @@ export function resolveCommandSession(input: unknown): CommandSession {
|
|
|
534
541
|
|| (typeof metadata?.endpoint === 'string' && metadata.endpoint ? metadata.endpoint : undefined);
|
|
535
542
|
|
|
536
543
|
const scene = resolveScene(input, metadata);
|
|
537
|
-
const sender =
|
|
544
|
+
const sender = resolveCommandSender(input, metadata);
|
|
538
545
|
|
|
539
546
|
return Object.freeze({
|
|
540
547
|
...(adapter ? { adapter } : {}),
|
|
@@ -583,8 +590,12 @@ function resolveScene(
|
|
|
583
590
|
});
|
|
584
591
|
}
|
|
585
592
|
|
|
586
|
-
|
|
587
|
-
|
|
593
|
+
/**
|
|
594
|
+
* Pure canonical sender projection shared with read-only command condition simulation.
|
|
595
|
+
* @internal Command dispatcher projection helper.
|
|
596
|
+
*/
|
|
597
|
+
export function resolveCommandSender(
|
|
598
|
+
input: Pick<CommandMessage, 'sender'>,
|
|
588
599
|
metadata: Readonly<Record<string, unknown>> | undefined,
|
|
589
600
|
): CommandSender | undefined {
|
|
590
601
|
const structured = (input as { readonly from?: unknown }).from;
|
|
@@ -606,7 +617,7 @@ function resolveSender(
|
|
|
606
617
|
}
|
|
607
618
|
|
|
608
619
|
function resolveRoles(
|
|
609
|
-
input: CommandMessage,
|
|
620
|
+
input: Pick<CommandMessage, 'sender'>,
|
|
610
621
|
metadata: Readonly<Record<string, unknown>> | undefined,
|
|
611
622
|
): readonly string[] {
|
|
612
623
|
if (input.sender?.roles?.length) {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Optional per-capability budget, isolated by sender and the complete conversation identity. */
|
|
2
|
+
export interface CommandExecutionPolicy {
|
|
3
|
+
readonly cooldownMs?: number;
|
|
4
|
+
readonly maxConcurrent?: number;
|
|
5
|
+
/** One best-effort progress message while business logic is waiting; disabled by default. */
|
|
6
|
+
readonly progressAfterMs?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface CommandExecutionLimit {
|
|
10
|
+
readonly reason: 'cooldown' | 'busy' | 'capacity' | 'operation_unavailable' | 'identity_unavailable' | 'service_unavailable';
|
|
11
|
+
/** Exact remaining cooldown; a busy operation has no predictable completion time. */
|
|
12
|
+
readonly retryAfterMs?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class CommandExecutionLimitedError extends Error {
|
|
16
|
+
readonly limit: CommandExecutionLimit;
|
|
17
|
+
constructor(limit: CommandExecutionLimit) {
|
|
18
|
+
super(`Command execution limited: ${limit.reason}`);
|
|
19
|
+
this.name = 'CommandExecutionLimitedError';
|
|
20
|
+
this.limit = limit;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @internal Validate at declaration and projection, without coercion or silent clamping. */
|
|
25
|
+
export function normalizeExecutionPolicy(value: unknown): Readonly<CommandExecutionPolicy> | undefined {
|
|
26
|
+
if (value === undefined) return undefined;
|
|
27
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('Command execution must be an object');
|
|
28
|
+
const limits = { cooldownMs: 86_400_000, maxConcurrent: 1000, progressAfterMs: 3_600_000 };
|
|
29
|
+
const result: Record<string, number> = {};
|
|
30
|
+
for (const [key, item] of Object.entries(value)) {
|
|
31
|
+
if (!Object.hasOwn(limits, key) || !Number.isSafeInteger(item) || (item as number) <= 0
|
|
32
|
+
|| (item as number) > limits[key as keyof typeof limits]) {
|
|
33
|
+
throw new TypeError(`Command execution.${key} must be a positive bounded integer`);
|
|
34
|
+
}
|
|
35
|
+
result[key] = item as number;
|
|
36
|
+
}
|
|
37
|
+
if (Object.keys(result).length === 0) throw new TypeError('Command execution requires at least one policy');
|
|
38
|
+
return Object.freeze(result);
|
|
39
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/** Internal composition contract. Not part of the command authoring facade. */
|
|
2
|
+
import { createToken, type PluginId, type RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
3
|
+
import type { CommandExecutionLimit, CommandExecutionPolicy } from './execution-policy.js';
|
|
4
|
+
|
|
5
|
+
interface ConversationIdentity {
|
|
6
|
+
readonly endpoint: { readonly adapter: string; readonly id: string };
|
|
7
|
+
readonly kind: string;
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly parent?: { readonly kind: string; readonly id: string };
|
|
10
|
+
readonly threadId?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface AdmissionReader {
|
|
14
|
+
admit(source: unknown, capability: string, owner: PluginId, policy: CommandExecutionPolicy, snapshot: RuntimeSnapshot): CommandExecutionLimit | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @internal Admission-only Root resource; no bind or finish authority is published. */
|
|
18
|
+
export const commandExecutionAdmissionToken = createToken<AdmissionReader>('zhin.command.execution-admission');
|
|
19
|
+
|
|
20
|
+
/** @internal Held only by Core's actual leased operation. */
|
|
21
|
+
export interface CommandOperationController {
|
|
22
|
+
bind(view: object, owner: PluginId): void;
|
|
23
|
+
/** Core registers actual started delivery promises, never a caller-supplied completion callback. */
|
|
24
|
+
trackSend(send: Promise<unknown>): void;
|
|
25
|
+
/** Stop pending feedback and drain already-started feedback before the final reply. */
|
|
26
|
+
stopProgress(): Promise<void>;
|
|
27
|
+
/** Called only by the real outer operation's finally, after all final sends settle. */
|
|
28
|
+
finish(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Entry { active: number; cooldownUntil: number }
|
|
32
|
+
interface Operation {
|
|
33
|
+
snapshot?: RuntimeSnapshot;
|
|
34
|
+
key?: readonly string[];
|
|
35
|
+
active: boolean;
|
|
36
|
+
progressStopped: boolean;
|
|
37
|
+
admitted: boolean;
|
|
38
|
+
signal: AbortSignal;
|
|
39
|
+
reply?: (owner: PluginId, text: string) => Promise<unknown>;
|
|
40
|
+
readonly releases: (() => void)[];
|
|
41
|
+
readonly timers: Set<ReturnType<typeof setTimeout>>;
|
|
42
|
+
readonly feedback: Promise<void>[];
|
|
43
|
+
readonly accepted: Set<string>;
|
|
44
|
+
readonly sends: Set<Promise<void>>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @internal Root-instance-owned state shared by projections and draining retired generations. */
|
|
48
|
+
export class CommandExecutionBudget {
|
|
49
|
+
readonly #entries = new Map<string, Entry>();
|
|
50
|
+
readonly #views = new WeakMap<object, { operation: Operation; owner: PluginId }>();
|
|
51
|
+
readonly #clock: () => number;
|
|
52
|
+
readonly #capacity: number;
|
|
53
|
+
readonly admission: AdmissionReader;
|
|
54
|
+
|
|
55
|
+
constructor(options: { readonly clock?: () => number; readonly capacity?: number } = {}) {
|
|
56
|
+
this.#clock = options.clock ?? (() => performance.now());
|
|
57
|
+
this.#capacity = options.capacity ?? 10_000;
|
|
58
|
+
if (!Number.isSafeInteger(this.#capacity) || this.#capacity < 1) throw new TypeError('Invalid command budget capacity');
|
|
59
|
+
this.admission = Object.freeze<AdmissionReader>({ admit: (source, capability, owner, policy, snapshot) => this.#admit(source, capability, owner, policy, snapshot) });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
openOperation(input: {
|
|
63
|
+
readonly snapshot: RuntimeSnapshot;
|
|
64
|
+
readonly conversation: ConversationIdentity;
|
|
65
|
+
readonly senderId?: string;
|
|
66
|
+
readonly retirementSignal: AbortSignal;
|
|
67
|
+
readonly reply: (owner: PluginId, text: string) => Promise<unknown>;
|
|
68
|
+
}): CommandOperationController {
|
|
69
|
+
const c = input.conversation;
|
|
70
|
+
const values = [c.endpoint.adapter, c.endpoint.id, c.kind, c.id, c.parent?.kind ?? '', c.parent?.id ?? '', c.threadId ?? '', input.senderId ?? ''];
|
|
71
|
+
const valid = values.slice(0, 4).every(nonempty) && ['private', 'group', 'channel'].includes(c.kind) && nonempty(input.senderId)
|
|
72
|
+
&& (c.parent === undefined || (nonempty(c.parent.kind) && nonempty(c.parent.id)))
|
|
73
|
+
&& (c.threadId === undefined || nonempty(c.threadId)) && values.every(value => value.length <= 1024);
|
|
74
|
+
const operation: Operation = {
|
|
75
|
+
snapshot: input.snapshot, key: valid ? Object.freeze(values) : undefined,
|
|
76
|
+
active: true, progressStopped: false, admitted: false, signal: input.retirementSignal,
|
|
77
|
+
reply: input.reply, releases: [], timers: new Set(), feedback: [], accepted: new Set(), sends: new Set(),
|
|
78
|
+
};
|
|
79
|
+
const stop = () => {
|
|
80
|
+
operation.progressStopped = true;
|
|
81
|
+
for (const timer of operation.timers) clearTimeout(timer);
|
|
82
|
+
operation.timers.clear();
|
|
83
|
+
};
|
|
84
|
+
input.retirementSignal.addEventListener('abort', stop, { once: true });
|
|
85
|
+
return Object.freeze({
|
|
86
|
+
bind: (view: object, owner: PluginId) => { if (operation.active) this.#views.set(view, { operation, owner }); },
|
|
87
|
+
trackSend: (send: Promise<unknown>) => {
|
|
88
|
+
if (!operation.active) return;
|
|
89
|
+
const settled = send.then(() => { operation.sends.delete(settled); }, () => { operation.sends.delete(settled); });
|
|
90
|
+
operation.sends.add(settled);
|
|
91
|
+
},
|
|
92
|
+
stopProgress: async () => { stop(); await Promise.all(operation.feedback); },
|
|
93
|
+
finish: async () => {
|
|
94
|
+
stop();
|
|
95
|
+
await Promise.all(operation.feedback);
|
|
96
|
+
// New sends may start while an earlier send is draining. Do not release from an initial snapshot alone.
|
|
97
|
+
if (operation.admitted) while (operation.sends.size > 0) await Promise.all(operation.sends);
|
|
98
|
+
if (!operation.active) return;
|
|
99
|
+
operation.active = false;
|
|
100
|
+
operation.signal.removeEventListener('abort', stop);
|
|
101
|
+
for (const release of operation.releases) release();
|
|
102
|
+
operation.releases.length = 0;
|
|
103
|
+
operation.feedback.length = 0;
|
|
104
|
+
operation.accepted.clear();
|
|
105
|
+
operation.sends.clear();
|
|
106
|
+
operation.reply = undefined;
|
|
107
|
+
operation.snapshot = undefined;
|
|
108
|
+
operation.key = undefined;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#admit(source: unknown, capability: string, owner: PluginId, policy: CommandExecutionPolicy, snapshot: RuntimeSnapshot): CommandExecutionLimit | undefined {
|
|
114
|
+
const binding = source !== null && typeof source === 'object' ? this.#views.get(source) : undefined;
|
|
115
|
+
const operation = binding?.operation;
|
|
116
|
+
if (!operation?.active || operation.snapshot !== snapshot || binding?.owner !== owner || operation.signal.aborted) return limited('operation_unavailable');
|
|
117
|
+
if (!operation.key) return limited('identity_unavailable');
|
|
118
|
+
const key = JSON.stringify([capability, ...operation.key]);
|
|
119
|
+
// Re-entering the same capability is a second attempt, rather than a second free execution.
|
|
120
|
+
const now = this.#clock();
|
|
121
|
+
let entry = this.#entries.get(key);
|
|
122
|
+
if (entry && policy.maxConcurrent !== undefined && entry.active >= policy.maxConcurrent) return limited('busy');
|
|
123
|
+
if (entry && entry.cooldownUntil > now) return Object.freeze({ reason: 'cooldown', retryAfterMs: Math.ceil(entry.cooldownUntil - now) });
|
|
124
|
+
if (!entry) {
|
|
125
|
+
if (this.#entries.size >= this.#capacity) {
|
|
126
|
+
for (const [oldKey, old] of this.#entries) if (old.active === 0 && old.cooldownUntil <= now) this.#entries.delete(oldKey);
|
|
127
|
+
if (this.#entries.size >= this.#capacity) return limited('capacity');
|
|
128
|
+
}
|
|
129
|
+
entry = { active: 0, cooldownUntil: 0 };
|
|
130
|
+
this.#entries.set(key, entry);
|
|
131
|
+
}
|
|
132
|
+
entry.active++;
|
|
133
|
+
operation.admitted = true;
|
|
134
|
+
entry.cooldownUntil = now + (policy.cooldownMs ?? 0);
|
|
135
|
+
const acceptedEntry = entry;
|
|
136
|
+
operation.releases.push(() => {
|
|
137
|
+
acceptedEntry.active--;
|
|
138
|
+
if (acceptedEntry.active === 0 && acceptedEntry.cooldownUntil <= this.#clock()) this.#entries.delete(key);
|
|
139
|
+
});
|
|
140
|
+
if (policy.progressAfterMs !== undefined && !operation.progressStopped && !operation.accepted.has(capability)) {
|
|
141
|
+
const timer = setTimeout(() => {
|
|
142
|
+
operation.timers.delete(timer);
|
|
143
|
+
if (!operation.active || operation.progressStopped || operation.signal.aborted || !operation.reply) return;
|
|
144
|
+
// Observe failure once. Never retry uncertain delivery and never expose the exception.
|
|
145
|
+
let send: Promise<void>;
|
|
146
|
+
try { send = Promise.resolve(operation.reply(owner, '正在处理中,请稍候。')).then(() => {}, () => {}); }
|
|
147
|
+
catch { send = Promise.resolve(); }
|
|
148
|
+
operation.feedback.push(send);
|
|
149
|
+
}, policy.progressAfterMs);
|
|
150
|
+
operation.timers.add(timer);
|
|
151
|
+
}
|
|
152
|
+
operation.accepted.add(capability);
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function nonempty(value: unknown): value is string { return typeof value === 'string' && value.length > 0; }
|
|
158
|
+
function limited(reason: CommandExecutionLimit['reason']): CommandExecutionLimit { return Object.freeze({ reason }); }
|
|
159
|
+
|
|
160
|
+
/** @internal Projection reads the Root resource; ordinary authoring values cannot bind an operation. */
|
|
161
|
+
export function admitCommandExecution(source: unknown, capability: string, owner: PluginId, policy: CommandExecutionPolicy | undefined, snapshot: RuntimeSnapshot): CommandExecutionLimit | undefined {
|
|
162
|
+
if (!policy) return undefined;
|
|
163
|
+
const reader = snapshot.resources.get(snapshot.root)?.get(commandExecutionAdmissionToken.id) as AdmissionReader | undefined;
|
|
164
|
+
return reader ? reader.admit(source, capability, owner, policy, snapshot) : limited('service_unavailable');
|
|
165
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { operationClientAdapter } from '@zhin.js/feature-kit';
|
|
2
|
+
import { explainPermitRules, toPermissionSubject, type PermissionSubject, type PermissionConditionExplanation } from '@zhin.js/permission';
|
|
3
|
+
import { resolveCommandSender, resolveCommandSession, type CommandMessage } from './definition.js';
|
|
4
|
+
import type { CommandDescriptor } from './command-index.js';
|
|
5
|
+
|
|
6
|
+
/** The exact subject projection used by the real command permit gate. No factories or checkers run. */
|
|
7
|
+
export function toCommandPermissionSubject(source: unknown): PermissionSubject {
|
|
8
|
+
const projected = toPermissionSubject(source);
|
|
9
|
+
const session = resolveCommandSession(source);
|
|
10
|
+
return { ...projected, ...session, ...(session.sender ? { sender: { ...projected.sender, ...session.sender } } : {}) };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Exact authoring adapter restriction, distinct from builtin adapter(...) matching. */
|
|
14
|
+
export function commandAdapterMatches(command: Readonly<{ adapter?: string }>, source: unknown): boolean {
|
|
15
|
+
return !command.adapter || operationClientAdapter(source) === command.adapter;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type CommandPermissionHostStatus = 'present' | 'missing' | 'unverified';
|
|
19
|
+
|
|
20
|
+
/** Read-only hypothetical conditions. A match is not permission to execute the command. */
|
|
21
|
+
export function explainCommandPermissions(
|
|
22
|
+
command: Pick<CommandDescriptor, 'adapter' | 'permit'>,
|
|
23
|
+
options: Readonly<{ source?: unknown; rolesKnown?: boolean; permissionHost: CommandPermissionHostStatus }>,
|
|
24
|
+
) {
|
|
25
|
+
const projected = toCommandPermissionSubject(options.source);
|
|
26
|
+
const inputSender = options.source && typeof options.source === 'object'
|
|
27
|
+
? (options.source as Pick<CommandMessage, 'sender'>).sender : undefined;
|
|
28
|
+
const structuredRoles = inputSender && typeof inputSender === 'object'
|
|
29
|
+
&& Array.isArray((inputSender as { readonly role?: unknown }).role);
|
|
30
|
+
// A sender-only assumption has no synthetic conversation. Apply the real canonical
|
|
31
|
+
// sender defaults without inventing an Endpoint or treating unknown roles as known.
|
|
32
|
+
const senderOnly = options.source && typeof options.source === 'object' && !('conversation' in options.source) && !structuredRoles
|
|
33
|
+
? resolveCommandSender(options.source as Pick<CommandMessage, 'sender'>, undefined) : undefined;
|
|
34
|
+
const subject = senderOnly ? { ...projected, sender: senderOnly } : projected;
|
|
35
|
+
const defaultedRoles = options.rolesKnown && Array.isArray(inputSender?.roles) && inputSender.roles.length === 0
|
|
36
|
+
&& subject.sender?.role.length === 1 && subject.sender.role[0] === 'user';
|
|
37
|
+
const actualAdapter = operationClientAdapter(options.source);
|
|
38
|
+
const adapter: PermissionConditionExplanation = !command.adapter
|
|
39
|
+
? { outcome: 'not_evaluated', reason: '命令未声明 adapter 约束' }
|
|
40
|
+
: !actualAdapter ? { outcome: 'unknown', reason: '未选择当前 Endpoint,无法判断命令的精确 adapter 约束' }
|
|
41
|
+
: { outcome: commandAdapterMatches(command, options.source) ? 'match' : 'mismatch', reason: '命令 adapter 与当前 Endpoint 声明的原生适配器精确比较;未探测在线状态' };
|
|
42
|
+
const rules = explainPermitRules(command.permit ?? [], subject, { rolesKnown: options.rolesKnown });
|
|
43
|
+
const conditions = [...(command.adapter ? [adapter] : []), ...rules];
|
|
44
|
+
let summary: PermissionConditionExplanation;
|
|
45
|
+
if (conditions.some(item => item.outcome === 'mismatch')) summary = { outcome: 'mismatch', reason: '至少一个声明条件与假设输入不符;这不是实际准入检查' };
|
|
46
|
+
else if ((rules.length > 0 && options.permissionHost !== 'present') || conditions.some(item => item.outcome === 'unknown')) {
|
|
47
|
+
summary = { outcome: 'unknown', reason: rules.length > 0 && options.permissionHost !== 'present'
|
|
48
|
+
? 'PermissionHost 缺失或无法确认;不能推断命令可执行' : '存在缺少依据的条件,不能推断命令可执行' };
|
|
49
|
+
} else if (rules.length === 0) summary = { outcome: 'not_evaluated', reason: '命令未声明 permit 限制;这不表示已获得执行权限' };
|
|
50
|
+
else if (conditions.some(item => item.outcome === 'not_evaluated')) summary = { outcome: 'not_evaluated', reason: '存在未执行的平台或自定义条件,不能推断命令可执行' };
|
|
51
|
+
else summary = { outcome: 'match', reason: '已解释的 builtin 条件与假设输入匹配;不代表真实授权或命令可执行' };
|
|
52
|
+
return Object.freeze({ permissionHost: options.permissionHost, adapter: Object.freeze(adapter), rules, summary: Object.freeze(summary),
|
|
53
|
+
unconstrained: Object.freeze(['已解释的 builtin 规则不检查具体 Endpoint、parent 或 thread;保留完整目标不等于这些维度受规则限制']),
|
|
54
|
+
assumptions: Object.freeze(['身份与角色仅用于模拟,未向平台核实', '未执行命令、动态默认值、平台或自定义 checker',
|
|
55
|
+
...(defaultedRoles
|
|
56
|
+
? ['canonical 输入未附角色时按命令运行时默认 user 解释;这不是已核验的平台角色'] : [])]),
|
|
57
|
+
});
|
|
58
|
+
}
|