@ai-pip/core 0.2.0 → 0.4.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/CHANGELOG.md +106 -3
- package/README.md +52 -951
- package/dist/AAL/constants.d.ts +15 -0
- package/dist/AAL/constants.d.ts.map +1 -0
- package/dist/AAL/constants.js +20 -0
- package/dist/AAL/constants.js.map +1 -0
- package/dist/AAL/index.d.ts +5 -4
- package/dist/AAL/index.d.ts.map +1 -1
- package/dist/AAL/index.js +4 -2
- package/dist/AAL/index.js.map +1 -1
- package/dist/AAL/process/applyRemovalPlan.d.ts +23 -0
- package/dist/AAL/process/applyRemovalPlan.d.ts.map +1 -0
- package/dist/AAL/process/applyRemovalPlan.js +157 -0
- package/dist/AAL/process/applyRemovalPlan.js.map +1 -0
- package/dist/AAL/process/buildDecisionReason.d.ts.map +1 -1
- package/dist/AAL/process/buildDecisionReason.js +24 -4
- package/dist/AAL/process/buildDecisionReason.js.map +1 -1
- package/dist/AAL/process/buildRemediationPlan.d.ts +22 -0
- package/dist/AAL/process/buildRemediationPlan.d.ts.map +1 -0
- package/dist/AAL/process/buildRemediationPlan.js +81 -0
- package/dist/AAL/process/buildRemediationPlan.js.map +1 -0
- package/dist/AAL/process/buildRemovalPlan.d.ts +27 -9
- package/dist/AAL/process/buildRemovalPlan.d.ts.map +1 -1
- package/dist/AAL/process/buildRemovalPlan.js +95 -29
- package/dist/AAL/process/buildRemovalPlan.js.map +1 -1
- package/dist/AAL/process/index.d.ts +2 -2
- package/dist/AAL/process/index.d.ts.map +1 -1
- package/dist/AAL/process/index.js +2 -1
- package/dist/AAL/process/index.js.map +1 -1
- package/dist/AAL/process/resolveAgentAction.d.ts.map +1 -1
- package/dist/AAL/process/resolveAgentAction.js +13 -0
- package/dist/AAL/process/resolveAgentAction.js.map +1 -1
- package/dist/AAL/process/validatePolicy.d.ts +20 -0
- package/dist/AAL/process/validatePolicy.d.ts.map +1 -0
- package/dist/AAL/process/validatePolicy.js +40 -0
- package/dist/AAL/process/validatePolicy.js.map +1 -0
- package/dist/AAL/types.d.ts +18 -31
- package/dist/AAL/types.d.ts.map +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/isl/detect/detect.d.ts +39 -0
- package/dist/isl/detect/detect.d.ts.map +1 -0
- package/dist/isl/detect/detect.js +369 -0
- package/dist/isl/detect/detect.js.map +1 -0
- package/dist/isl/detect/index.d.ts +6 -0
- package/dist/isl/detect/index.d.ts.map +1 -0
- package/dist/isl/detect/index.js +5 -0
- package/dist/isl/detect/index.js.map +1 -0
- package/dist/isl/index.d.ts +8 -1
- package/dist/isl/index.d.ts.map +1 -1
- package/dist/isl/index.js +5 -0
- package/dist/isl/index.js.map +1 -1
- package/dist/isl/process/emitSignal.d.ts +19 -10
- package/dist/isl/process/emitSignal.d.ts.map +1 -1
- package/dist/isl/process/emitSignal.js +25 -23
- package/dist/isl/process/emitSignal.js.map +1 -1
- package/dist/isl/process/index.d.ts +1 -0
- package/dist/isl/process/index.d.ts.map +1 -1
- package/dist/isl/riskScore/calculators.d.ts +19 -0
- package/dist/isl/riskScore/calculators.d.ts.map +1 -0
- package/dist/isl/riskScore/calculators.js +50 -0
- package/dist/isl/riskScore/calculators.js.map +1 -0
- package/dist/isl/riskScore/index.d.ts +14 -0
- package/dist/isl/riskScore/index.d.ts.map +1 -0
- package/dist/isl/riskScore/index.js +26 -0
- package/dist/isl/riskScore/index.js.map +1 -0
- package/dist/isl/riskScore/types.d.ts +20 -0
- package/dist/isl/riskScore/types.d.ts.map +1 -0
- package/dist/isl/riskScore/types.js +12 -0
- package/dist/isl/riskScore/types.js.map +1 -0
- package/dist/isl/sanitize.d.ts +8 -1
- package/dist/isl/sanitize.d.ts.map +1 -1
- package/dist/isl/sanitize.js +13 -5
- package/dist/isl/sanitize.js.map +1 -1
- package/dist/isl/signals.d.ts +16 -1
- package/dist/isl/signals.d.ts.map +1 -1
- package/dist/isl/signals.js +4 -2
- package/dist/isl/signals.js.map +1 -1
- package/dist/isl/value-objects/Pattern.d.ts +21 -0
- package/dist/isl/value-objects/Pattern.d.ts.map +1 -1
- package/dist/isl/value-objects/Pattern.js +36 -0
- package/dist/isl/value-objects/Pattern.js.map +1 -1
- package/dist/isl/value-objects/index.d.ts +2 -2
- package/dist/isl/value-objects/index.d.ts.map +1 -1
- package/dist/isl/value-objects/index.js +1 -1
- package/dist/isl/value-objects/index.js.map +1 -1
- package/dist/shared/audit.d.ts +126 -28
- package/dist/shared/audit.d.ts.map +1 -1
- package/dist/shared/audit.js +322 -44
- package/dist/shared/audit.js.map +1 -1
- package/dist/shared/envelope/envelope.d.ts +23 -0
- package/dist/shared/envelope/envelope.d.ts.map +1 -0
- package/dist/shared/envelope/envelope.js +58 -0
- package/dist/shared/envelope/envelope.js.map +1 -0
- package/dist/shared/envelope/exceptions/EnvelopeError.d.ts +8 -0
- package/dist/shared/envelope/exceptions/EnvelopeError.d.ts.map +1 -0
- package/dist/shared/envelope/exceptions/EnvelopeError.js +13 -0
- package/dist/shared/envelope/exceptions/EnvelopeError.js.map +1 -0
- package/dist/shared/envelope/exceptions/index.d.ts +2 -0
- package/dist/shared/envelope/exceptions/index.d.ts.map +1 -0
- package/dist/shared/envelope/exceptions/index.js +2 -0
- package/dist/shared/envelope/exceptions/index.js.map +1 -0
- package/dist/shared/envelope/index.d.ts +18 -0
- package/dist/shared/envelope/index.d.ts.map +1 -0
- package/dist/shared/envelope/index.js +15 -0
- package/dist/shared/envelope/index.js.map +1 -0
- package/dist/shared/envelope/types.d.ts +45 -0
- package/dist/shared/envelope/types.d.ts.map +1 -0
- package/dist/shared/envelope/types.js +10 -0
- package/dist/shared/envelope/types.js.map +1 -0
- package/dist/shared/envelope/value-objects/Metadata.d.ts +27 -0
- package/dist/shared/envelope/value-objects/Metadata.d.ts.map +1 -0
- package/dist/shared/envelope/value-objects/Metadata.js +57 -0
- package/dist/shared/envelope/value-objects/Metadata.js.map +1 -0
- package/dist/shared/envelope/value-objects/Nonce.d.ts +26 -0
- package/dist/shared/envelope/value-objects/Nonce.d.ts.map +1 -0
- package/dist/shared/envelope/value-objects/Nonce.js +38 -0
- package/dist/shared/envelope/value-objects/Nonce.js.map +1 -0
- package/dist/shared/envelope/value-objects/Signature.d.ts +28 -0
- package/dist/shared/envelope/value-objects/Signature.d.ts.map +1 -0
- package/dist/shared/envelope/value-objects/Signature.js +50 -0
- package/dist/shared/envelope/value-objects/Signature.js.map +1 -0
- package/dist/shared/envelope/value-objects/index.d.ts +9 -0
- package/dist/shared/envelope/value-objects/index.d.ts.map +1 -0
- package/dist/shared/envelope/value-objects/index.js +7 -0
- package/dist/shared/envelope/value-objects/index.js.map +1 -0
- package/dist/shared/index.d.ts +2 -2
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index.js +1 -1
- package/dist/shared/index.js.map +1 -1
- package/package.json +11 -6
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AAL display constants - semantic mapping for SDK/UI.
|
|
3
|
+
* Used for consistent display of AAL decisions (e.g. colors).
|
|
4
|
+
*/
|
|
5
|
+
import type { AnomalyAction } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Display color for each AAL action.
|
|
8
|
+
* SDK/UI can use this for consistent styling (e.g. green = allow, yellow = warn, red = block).
|
|
9
|
+
*/
|
|
10
|
+
export declare const ACTION_DISPLAY_COLORS: Record<AnomalyAction, string>;
|
|
11
|
+
/**
|
|
12
|
+
* Returns the display color for an AAL action.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getActionDisplayColor(action: AnomalyAction): string;
|
|
15
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/AAL/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,MAAM,CAAC,aAAa,EAAE,MAAM,CAI9D,CAAA;AAEF;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,CAEnE"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AAL display constants - semantic mapping for SDK/UI.
|
|
3
|
+
* Used for consistent display of AAL decisions (e.g. colors).
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Display color for each AAL action.
|
|
7
|
+
* SDK/UI can use this for consistent styling (e.g. green = allow, yellow = warn, red = block).
|
|
8
|
+
*/
|
|
9
|
+
export const ACTION_DISPLAY_COLORS = Object.freeze({
|
|
10
|
+
ALLOW: 'green',
|
|
11
|
+
WARN: 'yellow',
|
|
12
|
+
BLOCK: 'red'
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Returns the display color for an AAL action.
|
|
16
|
+
*/
|
|
17
|
+
export function getActionDisplayColor(action) {
|
|
18
|
+
return ACTION_DISPLAY_COLORS[action];
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/AAL/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAkC,MAAM,CAAC,MAAM,CAAC;IAChF,KAAK,EAAE,OAAO;IACd,IAAI,EAAE,QAAQ;IACd,KAAK,EAAE,KAAK;CACb,CAAC,CAAA;AAEF;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAqB;IACzD,OAAO,qBAAqB,CAAC,MAAM,CAAC,CAAA;AACtC,CAAC"}
|
package/dist/AAL/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* **Architecture:**
|
|
12
12
|
* - Consumes ISLSignal (not ISLResult) to maintain layer separation
|
|
13
13
|
* - Applies configurable policies (ALLOW/WARN/BLOCK)
|
|
14
|
-
* - Builds
|
|
14
|
+
* - Builds remediation plans (what to do, not how; SDK/AI agent performs cleanup)
|
|
15
15
|
* - Does not execute actions (that is SDK responsibility)
|
|
16
16
|
*
|
|
17
17
|
* **Does NOT contain:**
|
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
*/
|
|
22
22
|
export { createAnomalyScore, isHighRisk, isLowRisk, isWarnRisk, isRoleProtected, isContextLeakPreventionEnabled, isInstructionImmutable, isIntentBlocked, isScopeSensitive } from './value-objects/index.js';
|
|
23
23
|
export type { AnomalyScore, PolicyRule } from './value-objects/index.js';
|
|
24
|
-
export { resolveAgentAction, resolveAgentActionWithScore, buildDecisionReason,
|
|
25
|
-
export type { DecisionReason
|
|
24
|
+
export { resolveAgentAction, resolveAgentActionWithScore, buildDecisionReason, buildRemediationPlan, validateAgentPolicyThresholds } from './process/index.js';
|
|
25
|
+
export type { DecisionReason } from './process/index.js';
|
|
26
26
|
export { buildAALLineage } from './lineage/index.js';
|
|
27
|
-
export
|
|
27
|
+
export { ACTION_DISPLAY_COLORS, getActionDisplayColor } from './constants.js';
|
|
28
|
+
export type { AnomalyAction, RemediationPlan, BlockedIntent, SensitiveScope, ProtectedRole, ImmutableInstruction, AgentPolicy, } from './types.js';
|
|
28
29
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/AAL/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/AAL/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EACH,kBAAkB,EAClB,UAAU,EACV,SAAS,EACT,UAAU,EACV,eAAe,EACf,8BAA8B,EAC9B,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EACnB,MAAM,0BAA0B,CAAA;AAEjC,YAAY,EACR,YAAY,EACZ,UAAU,EACb,MAAM,0BAA0B,CAAA;AAGjC,OAAO,EACH,kBAAkB,EAClB,2BAA2B,EAC3B,mBAAmB,EACnB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/AAL/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EACH,kBAAkB,EAClB,UAAU,EACV,SAAS,EACT,UAAU,EACV,eAAe,EACf,8BAA8B,EAC9B,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EACnB,MAAM,0BAA0B,CAAA;AAEjC,YAAY,EACR,YAAY,EACZ,UAAU,EACb,MAAM,0BAA0B,CAAA;AAGjC,OAAO,EACH,kBAAkB,EAClB,2BAA2B,EAC3B,mBAAmB,EACnB,oBAAoB,EACpB,6BAA6B,EAChC,MAAM,oBAAoB,CAAA;AAE3B,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAGxD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAGpD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AAG7E,YAAY,EACR,aAAa,EACb,eAAe,EACf,aAAa,EACb,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,WAAW,GACd,MAAM,YAAY,CAAA"}
|
package/dist/AAL/index.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* **Architecture:**
|
|
12
12
|
* - Consumes ISLSignal (not ISLResult) to maintain layer separation
|
|
13
13
|
* - Applies configurable policies (ALLOW/WARN/BLOCK)
|
|
14
|
-
* - Builds
|
|
14
|
+
* - Builds remediation plans (what to do, not how; SDK/AI agent performs cleanup)
|
|
15
15
|
* - Does not execute actions (that is SDK responsibility)
|
|
16
16
|
*
|
|
17
17
|
* **Does NOT contain:**
|
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
// Value objects
|
|
23
23
|
export { createAnomalyScore, isHighRisk, isLowRisk, isWarnRisk, isRoleProtected, isContextLeakPreventionEnabled, isInstructionImmutable, isIntentBlocked, isScopeSensitive } from './value-objects/index.js';
|
|
24
24
|
// Process functions
|
|
25
|
-
export { resolveAgentAction, resolveAgentActionWithScore, buildDecisionReason,
|
|
25
|
+
export { resolveAgentAction, resolveAgentActionWithScore, buildDecisionReason, buildRemediationPlan, validateAgentPolicyThresholds } from './process/index.js';
|
|
26
26
|
// Lineage
|
|
27
27
|
export { buildAALLineage } from './lineage/index.js';
|
|
28
|
+
// Display constants (for SDK/UI)
|
|
29
|
+
export { ACTION_DISPLAY_COLORS, getActionDisplayColor } from './constants.js';
|
|
28
30
|
//# sourceMappingURL=index.js.map
|
package/dist/AAL/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/AAL/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,gBAAgB;AAChB,OAAO,EACH,kBAAkB,EAClB,UAAU,EACV,SAAS,EACT,UAAU,EACV,eAAe,EACf,8BAA8B,EAC9B,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EACnB,MAAM,0BAA0B,CAAA;AAOjC,oBAAoB;AACpB,OAAO,EACH,kBAAkB,EAClB,2BAA2B,EAC3B,mBAAmB,EACnB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/AAL/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,gBAAgB;AAChB,OAAO,EACH,kBAAkB,EAClB,UAAU,EACV,SAAS,EACT,UAAU,EACV,eAAe,EACf,8BAA8B,EAC9B,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EACnB,MAAM,0BAA0B,CAAA;AAOjC,oBAAoB;AACpB,OAAO,EACH,kBAAkB,EAClB,2BAA2B,EAC3B,mBAAmB,EACnB,oBAAoB,EACpB,6BAA6B,EAChC,MAAM,oBAAoB,CAAA;AAI3B,UAAU;AACV,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEpD,iCAAiC;AACjC,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* applyRemovalPlan - Applies a removal plan to ISL result (pure, deterministic).
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* For each segment, only instructions with segmentId === segment.id are applied.
|
|
6
|
+
* Each instruction's position is in segment-local coordinates (indices within that
|
|
7
|
+
* segment's sanitizedContent). We remove only those ranges from that segment's string,
|
|
8
|
+
* then return a new ISLResult. Do not mix positions from one segment with another or
|
|
9
|
+
* use document-level offsets; this avoids corrupting legitimate content.
|
|
10
|
+
*/
|
|
11
|
+
import type { RemovalPlan } from './buildRemovalPlan.js';
|
|
12
|
+
import type { ISLResult } from '../../isl/types.js';
|
|
13
|
+
/**
|
|
14
|
+
* Applies a removal plan to an ISL result.
|
|
15
|
+
* Produces a new ISLResult with segment sanitizedContent updated (malicious ranges removed).
|
|
16
|
+
* Instructions without segmentId are ignored. Positions are clamped to [0, content.length] per segment; invalid or empty ranges are dropped. Lineage and metadata are preserved.
|
|
17
|
+
*
|
|
18
|
+
* @param islResult - ISL result (segments with sanitizedContent and optional piDetection)
|
|
19
|
+
* @param plan - Removal plan from buildRemovalPlanFromResult (must include segmentIds for removal)
|
|
20
|
+
* @returns New ISLResult with sanitizedContent updated per segment
|
|
21
|
+
*/
|
|
22
|
+
export declare function applyRemovalPlan(islResult: ISLResult, plan: RemovalPlan): ISLResult;
|
|
23
|
+
//# sourceMappingURL=applyRemovalPlan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applyRemovalPlan.d.ts","sourceRoot":"","sources":["../../../src/AAL/process/applyRemovalPlan.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,SAAS,EAAc,MAAM,oBAAoB,CAAA;AAwG/D;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,WAAW,GAAG,SAAS,CAwCnF"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* applyRemovalPlan - Applies a removal plan to ISL result (pure, deterministic).
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* For each segment, only instructions with segmentId === segment.id are applied.
|
|
6
|
+
* Each instruction's position is in segment-local coordinates (indices within that
|
|
7
|
+
* segment's sanitizedContent). We remove only those ranges from that segment's string,
|
|
8
|
+
* then return a new ISLResult. Do not mix positions from one segment with another or
|
|
9
|
+
* use document-level offsets; this avoids corrupting legitimate content.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Clamps position ranges to valid [0, contentLength) and drops empty or invalid ranges.
|
|
13
|
+
* Ensures 0 <= start <= end <= contentLength and start < end.
|
|
14
|
+
*/
|
|
15
|
+
function clampRangesToContent(ranges, contentLength) {
|
|
16
|
+
if (contentLength <= 0 || ranges.length === 0)
|
|
17
|
+
return [];
|
|
18
|
+
const result = [];
|
|
19
|
+
for (const r of ranges) {
|
|
20
|
+
const start = Math.max(0, Math.min(Number(r.start), contentLength));
|
|
21
|
+
const end = Math.max(0, Math.min(Number(r.end), contentLength));
|
|
22
|
+
if (start < end)
|
|
23
|
+
result.push({ start, end });
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Merges overlapping ranges (start inclusive, end exclusive). Sorted by start.
|
|
29
|
+
*/
|
|
30
|
+
function mergeRanges(ranges) {
|
|
31
|
+
if (ranges.length === 0)
|
|
32
|
+
return [];
|
|
33
|
+
const sorted = [...ranges].sort((a, b) => a.start - b.start);
|
|
34
|
+
const merged = [{ start: sorted[0].start, end: sorted[0].end }];
|
|
35
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
36
|
+
const r = sorted[i];
|
|
37
|
+
const last = merged.at(-1);
|
|
38
|
+
if (r.start <= last.end) {
|
|
39
|
+
merged[merged.length - 1] = { start: last.start, end: Math.max(last.end, r.end) };
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
merged.push({ start: r.start, end: r.end });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return merged;
|
|
46
|
+
}
|
|
47
|
+
/** Max gap (chars) between ranges to consider for merging when gap is only punctuation/whitespace */
|
|
48
|
+
const MAX_PUNCTUATION_GAP = 10;
|
|
49
|
+
/** True if the substring is only whitespace and/or common punctuation (no words). */
|
|
50
|
+
function isOnlyPunctuationOrWhitespace(s) {
|
|
51
|
+
return /^[\s.,;:!?'"-]*$/.test(s);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Merges consecutive ranges when the gap between them is only punctuation/whitespace,
|
|
55
|
+
* so we remove one contiguous block instead of leaving fragments like ", . ".
|
|
56
|
+
*/
|
|
57
|
+
function mergeAdjacentByPunctuation(ranges, content) {
|
|
58
|
+
if (ranges.length <= 1)
|
|
59
|
+
return [...ranges];
|
|
60
|
+
const result = [{ ...ranges[0] }];
|
|
61
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
62
|
+
const r = ranges[i];
|
|
63
|
+
const last = result.at(-1);
|
|
64
|
+
const gapStart = last.end;
|
|
65
|
+
const gapEnd = r.start;
|
|
66
|
+
if (gapEnd > gapStart && gapEnd - gapStart <= MAX_PUNCTUATION_GAP) {
|
|
67
|
+
const gap = content.slice(gapStart, gapEnd);
|
|
68
|
+
if (isOnlyPunctuationOrWhitespace(gap)) {
|
|
69
|
+
result[result.length - 1] = { start: last.start, end: r.end };
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
result.push({ ...r });
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Removes given ranges from content. Ranges must be non-overlapping and sorted by start.
|
|
79
|
+
*/
|
|
80
|
+
function removeRanges(content, ranges) {
|
|
81
|
+
if (ranges.length === 0)
|
|
82
|
+
return content;
|
|
83
|
+
let result = '';
|
|
84
|
+
let pos = 0;
|
|
85
|
+
for (const r of ranges) {
|
|
86
|
+
if (r.start > pos)
|
|
87
|
+
result += content.slice(pos, r.start);
|
|
88
|
+
pos = Math.max(pos, r.end);
|
|
89
|
+
}
|
|
90
|
+
if (pos < content.length)
|
|
91
|
+
result += content.slice(pos);
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
function assertApplyRemovalPlanArgs(islResult, plan) {
|
|
95
|
+
if (islResult == null || typeof islResult !== 'object') {
|
|
96
|
+
throw new TypeError('AAL applyRemovalPlan: islResult must be a non-null object');
|
|
97
|
+
}
|
|
98
|
+
if (!Array.isArray(islResult.segments)) {
|
|
99
|
+
throw new TypeError('AAL applyRemovalPlan: islResult.segments must be an array');
|
|
100
|
+
}
|
|
101
|
+
if (plan == null || typeof plan !== 'object') {
|
|
102
|
+
throw new TypeError('AAL applyRemovalPlan: plan must be a non-null object');
|
|
103
|
+
}
|
|
104
|
+
if (!Array.isArray(plan.instructionsToRemove)) {
|
|
105
|
+
throw new TypeError('AAL applyRemovalPlan: plan.instructionsToRemove must be an array');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Applies a removal plan to an ISL result.
|
|
110
|
+
* Produces a new ISLResult with segment sanitizedContent updated (malicious ranges removed).
|
|
111
|
+
* Instructions without segmentId are ignored. Positions are clamped to [0, content.length] per segment; invalid or empty ranges are dropped. Lineage and metadata are preserved.
|
|
112
|
+
*
|
|
113
|
+
* @param islResult - ISL result (segments with sanitizedContent and optional piDetection)
|
|
114
|
+
* @param plan - Removal plan from buildRemovalPlanFromResult (must include segmentIds for removal)
|
|
115
|
+
* @returns New ISLResult with sanitizedContent updated per segment
|
|
116
|
+
*/
|
|
117
|
+
export function applyRemovalPlan(islResult, plan) {
|
|
118
|
+
assertApplyRemovalPlanArgs(islResult, plan);
|
|
119
|
+
if (!plan.shouldRemove || plan.instructionsToRemove.length === 0) {
|
|
120
|
+
return islResult;
|
|
121
|
+
}
|
|
122
|
+
// Group instructions by segmentId. Each instruction's position is segment-local
|
|
123
|
+
// (indices within that segment's sanitizedContent). We never use positions from
|
|
124
|
+
// one segment on another segment or any global document offset.
|
|
125
|
+
const bySegmentId = new Map();
|
|
126
|
+
for (const inst of plan.instructionsToRemove) {
|
|
127
|
+
if (inst.segmentId == null)
|
|
128
|
+
continue;
|
|
129
|
+
const list = bySegmentId.get(inst.segmentId) ?? [];
|
|
130
|
+
list.push(inst.position);
|
|
131
|
+
bySegmentId.set(inst.segmentId, list);
|
|
132
|
+
}
|
|
133
|
+
if (bySegmentId.size === 0)
|
|
134
|
+
return islResult;
|
|
135
|
+
// For each segment, take only instructions with segmentId === segment.id,
|
|
136
|
+
// apply those ranges only to this segment's sanitizedContent, produce new segment.
|
|
137
|
+
const newSegments = islResult.segments.map((seg) => {
|
|
138
|
+
const ranges = bySegmentId.get(seg.id);
|
|
139
|
+
if (ranges == null || ranges.length === 0)
|
|
140
|
+
return seg;
|
|
141
|
+
const content = seg.sanitizedContent ?? '';
|
|
142
|
+
const len = typeof content === 'string' ? content.length : 0;
|
|
143
|
+
const clamped = clampRangesToContent(ranges, len);
|
|
144
|
+
if (clamped.length === 0)
|
|
145
|
+
return seg;
|
|
146
|
+
const merged = mergeRanges(clamped);
|
|
147
|
+
const mergedAdjacent = mergeAdjacentByPunctuation(merged, content);
|
|
148
|
+
const newContent = removeRanges(content, mergedAdjacent);
|
|
149
|
+
return { ...seg, sanitizedContent: newContent };
|
|
150
|
+
});
|
|
151
|
+
return {
|
|
152
|
+
segments: Object.freeze(newSegments),
|
|
153
|
+
lineage: islResult.lineage,
|
|
154
|
+
metadata: islResult.metadata
|
|
155
|
+
};
|
|
156
|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AAW,CAAC;QACpC,OAAO,EAAE,SAAS,CAAC,OAAO;QAC1B,QAAQ,EAAE,SAAS,CAAC,QAAQ;KAC7B,CAAA;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildDecisionReason.d.ts","sourceRoot":"","sources":["../../../src/AAL/process/buildDecisionReason.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"buildDecisionReason.d.ts","sourceRoot":"","sources":["../../../src/AAL/process/buildDecisionReason.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAG9C;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAA;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAA;IAC5B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;CAChC;AAwBD;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,WAAW,GAClB,cAAc,CAgChB"}
|
|
@@ -10,6 +10,23 @@
|
|
|
10
10
|
* - Include risk score and threshold information
|
|
11
11
|
* - Facilitate auditing and debugging
|
|
12
12
|
*/
|
|
13
|
+
import { validateAgentPolicyThresholds } from './validatePolicy.js';
|
|
14
|
+
const VALID_ACTIONS = new Set(['ALLOW', 'WARN', 'BLOCK']);
|
|
15
|
+
function assertBuildDecisionReasonArgs(action, islSignal, policy) {
|
|
16
|
+
if (action == null || !VALID_ACTIONS.has(action)) {
|
|
17
|
+
throw new TypeError('AAL buildDecisionReason: action must be ALLOW, WARN, or BLOCK');
|
|
18
|
+
}
|
|
19
|
+
if (islSignal == null || typeof islSignal !== 'object') {
|
|
20
|
+
throw new TypeError('AAL buildDecisionReason: islSignal must be a non-null object');
|
|
21
|
+
}
|
|
22
|
+
if (typeof islSignal.riskScore !== 'number') {
|
|
23
|
+
throw new TypeError('AAL buildDecisionReason: islSignal.riskScore must be a number');
|
|
24
|
+
}
|
|
25
|
+
if (policy == null || typeof policy !== 'object') {
|
|
26
|
+
throw new TypeError('AAL buildDecisionReason: policy must be a non-null object');
|
|
27
|
+
}
|
|
28
|
+
validateAgentPolicyThresholds(policy);
|
|
29
|
+
}
|
|
13
30
|
/**
|
|
14
31
|
* Builds the reason for a decision
|
|
15
32
|
*
|
|
@@ -19,6 +36,9 @@
|
|
|
19
36
|
* @returns DecisionReason with complete information
|
|
20
37
|
*/
|
|
21
38
|
export function buildDecisionReason(action, islSignal, policy) {
|
|
39
|
+
assertBuildDecisionReasonArgs(action, islSignal, policy);
|
|
40
|
+
const detectionCount = islSignal.piDetection?.detections?.length ?? 0;
|
|
41
|
+
const hasThreats = islSignal.hasThreats === true && detectionCount > 0;
|
|
22
42
|
let threshold;
|
|
23
43
|
let reason;
|
|
24
44
|
if (action === 'BLOCK') {
|
|
@@ -33,16 +53,16 @@ export function buildDecisionReason(action, islSignal, policy) {
|
|
|
33
53
|
threshold = policy.thresholds.warn;
|
|
34
54
|
reason = `Risk score ${islSignal.riskScore.toFixed(3)} is below warn threshold ${threshold.toFixed(3)}`;
|
|
35
55
|
}
|
|
36
|
-
if (
|
|
37
|
-
reason += `. ${
|
|
56
|
+
if (hasThreats) {
|
|
57
|
+
reason += `. ${detectionCount} threat(s) detected.`;
|
|
38
58
|
}
|
|
39
59
|
return {
|
|
40
60
|
action,
|
|
41
61
|
riskScore: islSignal.riskScore,
|
|
42
62
|
threshold,
|
|
43
63
|
reason,
|
|
44
|
-
hasThreats
|
|
45
|
-
detectionCount
|
|
64
|
+
hasThreats,
|
|
65
|
+
detectionCount
|
|
46
66
|
};
|
|
47
67
|
}
|
|
48
68
|
//# sourceMappingURL=buildDecisionReason.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildDecisionReason.js","sourceRoot":"","sources":["../../../src/AAL/process/buildDecisionReason.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;
|
|
1
|
+
{"version":3,"file":"buildDecisionReason.js","sourceRoot":"","sources":["../../../src/AAL/process/buildDecisionReason.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,EAAE,6BAA6B,EAAE,MAAM,qBAAqB,CAAA;AAcnE,MAAM,aAAa,GAAG,IAAI,GAAG,CAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;AAExE,SAAS,6BAA6B,CACpC,MAAqB,EACrB,SAAoB,EACpB,MAAmB;IAEnB,IAAI,MAAM,IAAI,IAAI,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,SAAS,CAAC,+DAA+D,CAAC,CAAA;IACtF,CAAC;IACD,IAAI,SAAS,IAAI,IAAI,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QACvD,MAAM,IAAI,SAAS,CAAC,8DAA8D,CAAC,CAAA;IACrF,CAAC;IACD,IAAI,OAAO,SAAS,CAAC,SAAS,KAAK,QAAQ,EAAE,CAAC;QAC5C,MAAM,IAAI,SAAS,CAAC,+DAA+D,CAAC,CAAA;IACtF,CAAC;IACD,IAAI,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,UAAU,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,cAAc,sBAAsB,CAAA;IACrD,CAAC;IAED,OAAO;QACL,MAAM;QACN,SAAS,EAAE,SAAS,CAAC,SAAS;QAC9B,SAAS;QACT,MAAM;QACN,UAAU;QACV,cAAc;KACf,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* buildRemediationPlan - Builds a remediation plan (what to do, not how).
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* AAL describes *what* to do: strategy, goals, constraints, and which segments
|
|
6
|
+
* are affected. The SDK (or an AI agent) is responsible for *how* to perform
|
|
7
|
+
* cleanup (e.g. using an AI tool to remove malicious instructions without
|
|
8
|
+
* affecting legitimate content).
|
|
9
|
+
*/
|
|
10
|
+
import type { RemediationPlan } from '../types.js';
|
|
11
|
+
import type { AgentPolicy } from '../types.js';
|
|
12
|
+
import type { ISLResult } from '../../isl/types.js';
|
|
13
|
+
/**
|
|
14
|
+
* Builds a remediation plan from the ISL result and policy.
|
|
15
|
+
* Target segments are those with at least one detection; goals are derived from detection types.
|
|
16
|
+
*
|
|
17
|
+
* @param islResult - ISL result with segments and per-segment piDetection
|
|
18
|
+
* @param policy - Agent policy (remediation.enabled)
|
|
19
|
+
* @returns RemediationPlan for the SDK / AI agent to execute
|
|
20
|
+
*/
|
|
21
|
+
export declare function buildRemediationPlan(islResult: ISLResult, policy: AgentPolicy): RemediationPlan;
|
|
22
|
+
//# sourceMappingURL=buildRemediationPlan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildRemediationPlan.d.ts","sourceRoot":"","sources":["../../../src/AAL/process/buildRemediationPlan.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AA2CnD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,WAAW,GAClB,eAAe,CA+BjB"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* buildRemediationPlan - Builds a remediation plan (what to do, not how).
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* AAL describes *what* to do: strategy, goals, constraints, and which segments
|
|
6
|
+
* are affected. The SDK (or an AI agent) is responsible for *how* to perform
|
|
7
|
+
* cleanup (e.g. using an AI tool to remove malicious instructions without
|
|
8
|
+
* affecting legitimate content).
|
|
9
|
+
*/
|
|
10
|
+
import { validateAgentPolicyThresholds } from './validatePolicy.js';
|
|
11
|
+
const STRATEGY_AI_CLEANUP = 'AI_CLEANUP';
|
|
12
|
+
const DEFAULT_CONSTRAINTS = Object.freeze([
|
|
13
|
+
'preserve_user_intent',
|
|
14
|
+
'do_not_add_information',
|
|
15
|
+
'do_not_change_language'
|
|
16
|
+
]);
|
|
17
|
+
/** Maps ISL pattern_type to remediation goal (e.g. prompt-injection -> remove_prompt_injection). */
|
|
18
|
+
function patternTypeToGoal(type) {
|
|
19
|
+
const normalized = type.replaceAll('-', '_').toLowerCase();
|
|
20
|
+
return `remove_${normalized}`;
|
|
21
|
+
}
|
|
22
|
+
const EMPTY_PLAN = Object.freeze({
|
|
23
|
+
strategy: STRATEGY_AI_CLEANUP,
|
|
24
|
+
goals: Object.freeze([]),
|
|
25
|
+
constraints: DEFAULT_CONSTRAINTS,
|
|
26
|
+
targetSegments: Object.freeze([]),
|
|
27
|
+
needsRemediation: false
|
|
28
|
+
});
|
|
29
|
+
function assertBuildRemediationPlanArgs(islResult, policy) {
|
|
30
|
+
if (islResult == null || typeof islResult !== 'object') {
|
|
31
|
+
throw new TypeError('AAL buildRemediationPlan: islResult must be a non-null object');
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(islResult.segments)) {
|
|
34
|
+
throw new TypeError('AAL buildRemediationPlan: islResult.segments must be an array');
|
|
35
|
+
}
|
|
36
|
+
if (policy == null || typeof policy !== 'object') {
|
|
37
|
+
throw new TypeError('AAL buildRemediationPlan: policy must be a non-null object');
|
|
38
|
+
}
|
|
39
|
+
const r = policy.remediation;
|
|
40
|
+
if (r == null || typeof r !== 'object' || typeof r.enabled !== 'boolean') {
|
|
41
|
+
throw new TypeError('AAL buildRemediationPlan: policy.remediation.enabled must be a boolean');
|
|
42
|
+
}
|
|
43
|
+
validateAgentPolicyThresholds(policy);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Builds a remediation plan from the ISL result and policy.
|
|
47
|
+
* Target segments are those with at least one detection; goals are derived from detection types.
|
|
48
|
+
*
|
|
49
|
+
* @param islResult - ISL result with segments and per-segment piDetection
|
|
50
|
+
* @param policy - Agent policy (remediation.enabled)
|
|
51
|
+
* @returns RemediationPlan for the SDK / AI agent to execute
|
|
52
|
+
*/
|
|
53
|
+
export function buildRemediationPlan(islResult, policy) {
|
|
54
|
+
assertBuildRemediationPlanArgs(islResult, policy);
|
|
55
|
+
if (!policy.remediation.enabled) {
|
|
56
|
+
return EMPTY_PLAN;
|
|
57
|
+
}
|
|
58
|
+
const targetSegments = [];
|
|
59
|
+
const goalsSet = new Set();
|
|
60
|
+
for (const segment of islResult.segments) {
|
|
61
|
+
const detections = segment.piDetection?.detections;
|
|
62
|
+
if (!Array.isArray(detections) || detections.length === 0)
|
|
63
|
+
continue;
|
|
64
|
+
targetSegments.push(segment.id);
|
|
65
|
+
for (const d of detections) {
|
|
66
|
+
const type = d.pattern_type ?? 'unknown';
|
|
67
|
+
goalsSet.add(patternTypeToGoal(type));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (targetSegments.length === 0) {
|
|
71
|
+
return EMPTY_PLAN;
|
|
72
|
+
}
|
|
73
|
+
return Object.freeze({
|
|
74
|
+
strategy: STRATEGY_AI_CLEANUP,
|
|
75
|
+
goals: Object.freeze([...goalsSet].sort((a, b) => a.localeCompare(b))),
|
|
76
|
+
constraints: DEFAULT_CONSTRAINTS,
|
|
77
|
+
targetSegments: Object.freeze(targetSegments),
|
|
78
|
+
needsRemediation: true
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=buildRemediationPlan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildRemediationPlan.js","sourceRoot":"","sources":["../../../src/AAL/process/buildRemediationPlan.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAMH,OAAO,EAAE,6BAA6B,EAAE,MAAM,qBAAqB,CAAA;AAEnE,MAAM,mBAAmB,GAAG,YAAY,CAAA;AAExC,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,CAAC;IACxC,sBAAsB;IACtB,wBAAwB;IACxB,wBAAwB;CAChB,CAAC,CAAA;AAEX,oGAAoG;AACpG,SAAS,iBAAiB,CAAC,IAAY;IACrC,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,WAAW,EAAE,CAAA;IAC1D,OAAO,UAAU,UAAU,EAAE,CAAA;AAC/B,CAAC;AAED,MAAM,UAAU,GAAoB,MAAM,CAAC,MAAM,CAAC;IAChD,QAAQ,EAAE,mBAAmB;IAC7B,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;IACxB,WAAW,EAAE,mBAAmB;IAChC,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;IACjC,gBAAgB,EAAE,KAAK;CACxB,CAAC,CAAA;AAEF,SAAS,8BAA8B,CAAC,SAAoB,EAAE,MAAmB;IAC/E,IAAI,SAAS,IAAI,IAAI,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QACvD,MAAM,IAAI,SAAS,CAAC,+DAA+D,CAAC,CAAA;IACtF,CAAC;IACD,I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,SAAS,CAAA;YAChD,QAAQ,CAAC,GAAG,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAA;QACvC,CAAC;IACH,CAAC;IAED,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChC,OAAO,UAAU,CAAA;IACnB,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,QAAQ,EAAE,mBAAmB;QAC7B,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;QACtE,WAAW,EAAE,mBAAmB;QAChC,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC;QAC7C,gBAAgB,EAAE,IAAI;KACvB,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
* buildRemovalPlan - Builds a plan for instruction removal
|
|
3
3
|
*
|
|
4
4
|
* @remarks
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Each instruction in the plan must be tied to one segment and one range in that segment:
|
|
6
|
+
* - segmentId: same as segment.id in ISLResult (so applyRemovalPlan knows which segment to cut).
|
|
7
|
+
* - position: { start, end } in segment-local coordinates (indices within segment.sanitizedContent).
|
|
8
|
+
* So "remove instruction X" means: in the segment with id segmentId, delete exactly [start, end) of that segment's sanitizedContent.
|
|
7
9
|
*
|
|
8
|
-
*
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
11
|
-
* - Does not execute the removal (that is SDK responsibility)
|
|
10
|
+
* Two entry points:
|
|
11
|
+
* - buildRemovalPlan(islSignal, policy): from signal only; no segmentId (descriptive).
|
|
12
|
+
* - buildRemovalPlanFromResult(islResult, policy): from result; includes segmentId and segment-local position for applyRemovalPlan.
|
|
12
13
|
*/
|
|
13
14
|
import type { RemovedInstruction } from '../types.js';
|
|
14
|
-
import type { ISLSignal } from '../../isl/signals.js';
|
|
15
15
|
import type { AgentPolicy } from '../types.js';
|
|
16
|
+
import type { ISLSignal } from '../../isl/signals.js';
|
|
17
|
+
import type { ISLResult } from '../../isl/types.js';
|
|
16
18
|
/**
|
|
17
19
|
* Plan for instruction removal
|
|
18
20
|
*/
|
|
@@ -22,11 +24,27 @@ export interface RemovalPlan {
|
|
|
22
24
|
readonly removalEnabled: boolean;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
|
-
* Builds a plan for instruction removal
|
|
27
|
+
* Builds a plan for instruction removal from ISL signal (no segment ids).
|
|
28
|
+
* Use when you only have the signal; plan is descriptive. For actionable removal use buildRemovalPlanFromResult.
|
|
26
29
|
*
|
|
27
30
|
* @param islSignal - ISL signal with detections
|
|
28
31
|
* @param policy - Agent policy
|
|
29
|
-
* @returns RemovalPlan with instructions to remove
|
|
32
|
+
* @returns RemovalPlan with instructions to remove (no segmentId)
|
|
30
33
|
*/
|
|
31
34
|
export declare function buildRemovalPlan(islSignal: ISLSignal, policy: AgentPolicy): RemovalPlan;
|
|
35
|
+
/**
|
|
36
|
+
* Builds a plan for instruction removal from ISL result (with segment ids).
|
|
37
|
+
* Use with applyRemovalPlan to produce content with malicious ranges removed.
|
|
38
|
+
*
|
|
39
|
+
* Each instruction is built from one segment only:
|
|
40
|
+
* - segmentId = segment.id (so applyRemovalPlan knows which segment to cut).
|
|
41
|
+
* - position = detection.position as-is (already segment-local: indices within
|
|
42
|
+
* that segment's sanitizedContent, from detectThreats run on that segment).
|
|
43
|
+
* No document-concatenated or global offsets are used.
|
|
44
|
+
*
|
|
45
|
+
* @param islResult - ISL result with segments and per-segment piDetection
|
|
46
|
+
* @param policy - Agent policy
|
|
47
|
+
* @returns RemovalPlan with instructions to remove (segmentId + segment-local position per instruction)
|
|
48
|
+
*/
|
|
49
|
+
export declare function buildRemovalPlanFromResult(islResult: ISLResult, policy: AgentPolicy): RemovalPlan;
|
|
32
50
|
//# sourceMappingURL=buildRemovalPlan.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildRemovalPlan.d.ts","sourceRoot":"","sources":["../../../src/AAL/process/buildRemovalPlan.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"buildRemovalPlan.d.ts","sourceRoot":"","sources":["../../../src/AAL/process/buildRemovalPlan.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAGnD;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,oBAAoB,EAAE,SAAS,kBAAkB,EAAE,CAAA;IAC5D,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAA;IAC9B,QAAQ,CAAC,cAAc,EAAE,OAAO,CAAA;CACjC;AA0CD;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,WAAW,GAClB,WAAW,CAoBb;AAWD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,WAAW,GAClB,WAAW,CAoBb"}
|