@pilio/gemini-watermark-remover 1.0.39 → 1.0.40

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.
@@ -5,23 +5,23 @@ import { createRepairTrialFromStages } from './pipelineRepairTrial.js';
5
5
 
6
6
  const NEW_MARGIN_96_SIZE = 96;
7
7
  const NEW_MARGIN_96_MARGIN = 192;
8
- const HIGH_RISK_NEW_MARGIN_MIN_SPATIAL = 0.4;
9
- const HIGH_RISK_NEW_MARGIN_MIN_GRADIENT = 0.08;
10
- const SAFE_EXACT_NEW_MARGIN_MIN_SPATIAL = 0.18;
11
- const SAFE_EXACT_NEW_MARGIN_MIN_GRADIENT = 0.05;
12
- const SAFE_EXACT_NEW_MARGIN_MIN_IMPROVEMENT = 0.1;
13
- const SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_SPATIAL = 0.32;
14
- const SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_GRADIENT = 0.05;
8
+ const HIGH_RISK_NEW_MARGIN_MIN_SPATIAL = 0.4;
9
+ const HIGH_RISK_NEW_MARGIN_MIN_GRADIENT = 0.08;
10
+ const SAFE_EXACT_NEW_MARGIN_MIN_SPATIAL = 0.18;
11
+ const SAFE_EXACT_NEW_MARGIN_MIN_GRADIENT = 0.05;
12
+ const SAFE_EXACT_NEW_MARGIN_MIN_IMPROVEMENT = 0.1;
13
+ const SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_SPATIAL = 0.32;
14
+ const SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_GRADIENT = 0.05;
15
15
  const DEFAULT_ALPHA_NEW_MARGIN_MAX_SPATIAL_RESIDUAL = 0.18;
16
- const DEFAULT_ALPHA_NEW_MARGIN_MAX_GRADIENT_RESIDUAL = 0.08;
17
- const DEFAULT_ALPHA_NEW_MARGIN_MIN_IMPROVEMENT = 0.12;
18
- const DEFAULT_ALPHA_NEW_MARGIN_DAMAGE_ADVANTAGE = 0.03;
19
- const FULL_STRENGTH_NEW_MARGIN_MIN_ORIGINAL_GRADIENT = 0.08;
20
- const FULL_STRENGTH_NEW_MARGIN_MAX_PROCESSED_GRADIENT = 0.1;
21
- const FULL_STRENGTH_NEW_MARGIN_MIN_GRADIENT_ADVANTAGE = 0.03;
22
- const FULL_STRENGTH_NEW_MARGIN_MAX_CLIPPED_RATIO = 0.02;
23
- const UNSAFE_SHIFTED_MIN_STRONG_SPATIAL_EVIDENCE = 0.4;
24
- const UNSAFE_SHIFTED_MIN_STRONG_GRADIENT_EVIDENCE = 0.08;
16
+ const DEFAULT_ALPHA_NEW_MARGIN_MAX_GRADIENT_RESIDUAL = 0.08;
17
+ const DEFAULT_ALPHA_NEW_MARGIN_MIN_IMPROVEMENT = 0.12;
18
+ const DEFAULT_ALPHA_NEW_MARGIN_DAMAGE_ADVANTAGE = 0.03;
19
+ const FULL_STRENGTH_NEW_MARGIN_MIN_ORIGINAL_GRADIENT = 0.08;
20
+ const FULL_STRENGTH_NEW_MARGIN_MAX_PROCESSED_GRADIENT = 0.1;
21
+ const FULL_STRENGTH_NEW_MARGIN_MIN_GRADIENT_ADVANTAGE = 0.03;
22
+ const FULL_STRENGTH_NEW_MARGIN_MAX_CLIPPED_RATIO = 0.02;
23
+ const UNSAFE_SHIFTED_MIN_STRONG_SPATIAL_EVIDENCE = 0.4;
24
+ const UNSAFE_SHIFTED_MIN_STRONG_GRADIENT_EVIDENCE = 0.08;
25
25
 
26
26
  function numberOr(value, fallback = 0) {
27
27
  const number = Number(value);
@@ -49,30 +49,30 @@ export function isNewMarginAlphaVariantTrial(candidate) {
49
49
  config.alphaVariant === '20260520';
50
50
  }
51
51
 
52
- export function isDefaultAlphaNewMarginTrial(candidate) {
53
- const config = getConfig(candidate);
54
- return isNewMargin96Candidate(candidate) &&
55
- !config.alphaVariant;
56
- }
57
-
58
- function isNewMarginVariantFamilyCandidate(candidate) {
59
- const config = getConfig(candidate);
60
- const provenance = getProvenance(candidate);
61
- return config.marginRight === NEW_MARGIN_96_MARGIN &&
62
- config.marginBottom === NEW_MARGIN_96_MARGIN &&
63
- (config.alphaVariant === '20260520' || provenance.alphaVariant === '20260520');
64
- }
65
-
66
- function hasSafeExactNewMarginVariantRecovery(candidate) {
67
- if (!isNewMarginAlphaVariantTrial(candidate)) return false;
68
- if (candidate?.damage?.safe !== true) return false;
69
-
70
- return numberOr(candidate?.originalSpatialScore) >= SAFE_EXACT_NEW_MARGIN_MIN_SPATIAL &&
71
- numberOr(candidate?.originalGradientScore) >= SAFE_EXACT_NEW_MARGIN_MIN_GRADIENT &&
72
- numberOr(candidate?.improvement, -Infinity) >= SAFE_EXACT_NEW_MARGIN_MIN_IMPROVEMENT &&
73
- Math.abs(numberOr(candidate?.processedSpatialScore, Infinity)) <= SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_SPATIAL &&
74
- Math.max(0, numberOr(candidate?.processedGradientScore, Infinity)) <= SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_GRADIENT;
75
- }
52
+ export function isDefaultAlphaNewMarginTrial(candidate) {
53
+ const config = getConfig(candidate);
54
+ return isNewMargin96Candidate(candidate) &&
55
+ !config.alphaVariant;
56
+ }
57
+
58
+ function isNewMarginVariantFamilyCandidate(candidate) {
59
+ const config = getConfig(candidate);
60
+ const provenance = getProvenance(candidate);
61
+ return config.marginRight === NEW_MARGIN_96_MARGIN &&
62
+ config.marginBottom === NEW_MARGIN_96_MARGIN &&
63
+ (config.alphaVariant === '20260520' || provenance.alphaVariant === '20260520');
64
+ }
65
+
66
+ function hasSafeExactNewMarginVariantRecovery(candidate) {
67
+ if (!isNewMarginAlphaVariantTrial(candidate)) return false;
68
+ if (candidate?.damage?.safe !== true) return false;
69
+
70
+ return numberOr(candidate?.originalSpatialScore) >= SAFE_EXACT_NEW_MARGIN_MIN_SPATIAL &&
71
+ numberOr(candidate?.originalGradientScore) >= SAFE_EXACT_NEW_MARGIN_MIN_GRADIENT &&
72
+ numberOr(candidate?.improvement, -Infinity) >= SAFE_EXACT_NEW_MARGIN_MIN_IMPROVEMENT &&
73
+ Math.abs(numberOr(candidate?.processedSpatialScore, Infinity)) <= SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_SPATIAL &&
74
+ Math.max(0, numberOr(candidate?.processedGradientScore, Infinity)) <= SAFE_EXACT_NEW_MARGIN_MAX_PROCESSED_GRADIENT;
75
+ }
76
76
 
77
77
  export function hasClearedResidual(candidate) {
78
78
  return candidate?.residual?.cleared === true ||
@@ -89,36 +89,36 @@ export function hasSafeDefaultAlphaNewMarginResidual(candidate) {
89
89
  numberOr(candidate?.improvement, -Infinity) >= DEFAULT_ALPHA_NEW_MARGIN_MIN_IMPROVEMENT;
90
90
  }
91
91
 
92
- export function hasHighRiskNewMarginPositiveEvidence(candidate) {
93
- if (getProvenance(candidate).darkPolarity === true) return true;
94
- if (!isNewMarginVariantFamilyCandidate(candidate) && !isDefaultAlphaNewMarginTrial(candidate)) return true;
95
-
96
- const hasStrongEvidence = numberOr(candidate?.originalGradientScore) >= HIGH_RISK_NEW_MARGIN_MIN_GRADIENT ||
97
- numberOr(candidate?.originalSpatialScore) >= HIGH_RISK_NEW_MARGIN_MIN_SPATIAL;
98
- return hasStrongEvidence || hasSafeExactNewMarginVariantRecovery(candidate);
99
- }
92
+ export function hasHighRiskNewMarginPositiveEvidence(candidate) {
93
+ if (getProvenance(candidate).darkPolarity === true) return true;
94
+ if (!isNewMarginVariantFamilyCandidate(candidate) && !isDefaultAlphaNewMarginTrial(candidate)) return true;
95
+
96
+ const hasStrongEvidence = numberOr(candidate?.originalGradientScore) >= HIGH_RISK_NEW_MARGIN_MIN_GRADIENT ||
97
+ numberOr(candidate?.originalSpatialScore) >= HIGH_RISK_NEW_MARGIN_MIN_SPATIAL;
98
+ return hasStrongEvidence || hasSafeExactNewMarginVariantRecovery(candidate);
99
+ }
100
100
 
101
- export function shouldFailClosedForVisibleResidualUnsafeDamage({
101
+ export function shouldFailClosedForVisibleResidualUnsafeDamage({
102
102
  selectedTrial = null,
103
103
  residualVisibility = null
104
104
  } = {}) {
105
105
  return isNewMarginAlphaVariantTrial(selectedTrial) &&
106
106
  residualVisibility?.visible === true &&
107
- selectedTrial?.damage?.safe === false;
108
- }
109
-
110
- export function shouldFailClosedForUnsafeWeakShiftedCandidate({
111
- selectedTrial = null
112
- } = {}) {
113
- const provenance = getProvenance(selectedTrial);
114
- const isShiftedFallback = provenance.localShift === true || provenance.sizeJitter === true;
115
- const damageSafe = selectedTrial?.damage?.safe ?? selectedTrial?.evaluation?.damage?.safe;
116
-
117
- if (!isShiftedFallback || damageSafe !== false) return false;
118
-
119
- return numberOr(selectedTrial?.originalSpatialScore) < UNSAFE_SHIFTED_MIN_STRONG_SPATIAL_EVIDENCE &&
120
- numberOr(selectedTrial?.originalGradientScore) < UNSAFE_SHIFTED_MIN_STRONG_GRADIENT_EVIDENCE;
121
- }
107
+ selectedTrial?.damage?.safe === false;
108
+ }
109
+
110
+ export function shouldFailClosedForUnsafeWeakShiftedCandidate({
111
+ selectedTrial = null
112
+ } = {}) {
113
+ const provenance = getProvenance(selectedTrial);
114
+ const isShiftedFallback = provenance.localShift === true || provenance.sizeJitter === true;
115
+ const damageSafe = selectedTrial?.damage?.safe ?? selectedTrial?.evaluation?.damage?.safe;
116
+
117
+ if (!isShiftedFallback || damageSafe !== false) return false;
118
+
119
+ return numberOr(selectedTrial?.originalSpatialScore) < UNSAFE_SHIFTED_MIN_STRONG_SPATIAL_EVIDENCE &&
120
+ numberOr(selectedTrial?.originalGradientScore) < UNSAFE_SHIFTED_MIN_STRONG_GRADIENT_EVIDENCE;
121
+ }
122
122
 
123
123
  function firstFalseGate(gates) {
124
124
  for (const [name, allowed] of Object.entries(gates)) {
@@ -153,11 +153,11 @@ export function createCandidateEvaluation({
153
153
  provenance,
154
154
  originalSpatialScore: originalScores?.spatialScore,
155
155
  originalGradientScore: originalScores?.gradientScore,
156
- processedSpatialScore: processedScores?.spatialScore,
157
- processedGradientScore: processedScores?.gradientScore,
158
- improvement,
159
- damage
160
- };
156
+ processedSpatialScore: processedScores?.spatialScore,
157
+ processedGradientScore: processedScores?.gradientScore,
158
+ improvement,
159
+ damage
160
+ };
161
161
  const originalSpatial = numberOr(originalScores?.spatialScore);
162
162
  const originalGradient = numberOr(originalScores?.gradientScore);
163
163
  const processedSpatial = numberOr(processedScores?.spatialScore);
@@ -198,7 +198,7 @@ export function createCandidateEvaluation({
198
198
  };
199
199
  }
200
200
 
201
- export function shouldPreferDefaultAlphaNewMarginCandidate(currentBest, candidate) {
201
+ export function shouldPreferDefaultAlphaNewMarginCandidate(currentBest, candidate) {
202
202
  if (!isDefaultAlphaNewMarginTrial(candidate)) return false;
203
203
 
204
204
  if (isNewMarginAlphaVariantTrial(currentBest)) {
@@ -215,73 +215,73 @@ export function shouldPreferDefaultAlphaNewMarginCandidate(currentBest, candidat
215
215
  if (!Number.isFinite(currentDamage) || !Number.isFinite(candidateDamage)) return false;
216
216
 
217
217
  return candidateDamage + DEFAULT_ALPHA_NEW_MARGIN_DAMAGE_ADVANTAGE < currentDamage;
218
- }
219
-
220
- function hasSameExactNewMarginAnchor(fullStrength, conservative) {
221
- if (!isNewMarginAlphaVariantTrial(fullStrength) || !isNewMargin96Candidate(conservative)) {
222
- return false;
223
- }
224
- const fullPosition = fullStrength?.position ?? {};
225
- const conservativePosition = conservative?.position ?? {};
226
- return fullPosition.x === conservativePosition.x &&
227
- fullPosition.y === conservativePosition.y &&
228
- fullPosition.width === NEW_MARGIN_96_SIZE &&
229
- fullPosition.height === NEW_MARGIN_96_SIZE &&
230
- conservativePosition.width === NEW_MARGIN_96_SIZE &&
231
- conservativePosition.height === NEW_MARGIN_96_SIZE;
232
- }
233
-
234
- export function shouldPreferFullStrengthNewMarginVariant(fullStrength, conservative) {
235
- if (!hasSameExactNewMarginAnchor(fullStrength, conservative)) return false;
236
- if (numberOr(fullStrength?.alphaGain) !== 1) return false;
237
- if (numberOr(conservative?.alphaGain) >= 1) return false;
238
- if (getProvenance(fullStrength).darkPolarity === true) return false;
239
-
240
- const originalSpatial = numberOr(fullStrength?.originalSpatialScore, -Infinity);
241
- const originalGradient = numberOr(fullStrength?.originalGradientScore, -Infinity);
242
- const fullGradient = Math.max(0, numberOr(
243
- fullStrength?.processedGradientScore,
244
- Infinity
245
- ));
246
- const conservativeGradient = Math.max(0, numberOr(
247
- conservative?.processedGradientScore,
248
- Infinity
249
- ));
250
- const clippedRatio = numberOr(
251
- fullStrength?.damage?.newlyClippedRatio,
252
- Infinity
253
- );
254
- const provenance = getProvenance(fullStrength);
255
- const positiveLocalizedSourceWitness =
256
- provenance.sourceWitnessRescue === true &&
257
- numberOr(
258
- provenance.sourceWitnessGate?.spatialSignedTarget,
259
- -Infinity
260
- ) > 0;
261
-
262
- // The full-strength trial scores itself with the same alpha profile it
263
- // applies, so a negative spatial residual or noisy damage warning is not
264
- // independent rejection evidence. Require positive source polarity and
265
- // either a real gradient advantage or the decoy-localized source witness.
266
- return originalSpatial > 0 &&
267
- originalGradient >= FULL_STRENGTH_NEW_MARGIN_MIN_ORIGINAL_GRADIENT &&
268
- fullGradient <= FULL_STRENGTH_NEW_MARGIN_MAX_PROCESSED_GRADIENT &&
269
- (
270
- positiveLocalizedSourceWitness ||
271
- conservativeGradient - fullGradient >=
272
- FULL_STRENGTH_NEW_MARGIN_MIN_GRADIENT_ADVANTAGE
273
- ) &&
274
- clippedRatio <= FULL_STRENGTH_NEW_MARGIN_MAX_CLIPPED_RATIO;
275
- }
276
-
277
- export function arbitrateCandidateByEvaluation(currentBest, candidate) {
278
- if (shouldPreferFullStrengthNewMarginVariant(currentBest, candidate)) {
279
- return currentBest;
280
- }
281
- if (shouldPreferFullStrengthNewMarginVariant(candidate, currentBest)) {
282
- return candidate;
283
- }
284
- if (shouldPreferDefaultAlphaNewMarginCandidate(currentBest, candidate)) {
218
+ }
219
+
220
+ function hasSameExactNewMarginAnchor(fullStrength, conservative) {
221
+ if (!isNewMarginAlphaVariantTrial(fullStrength) || !isNewMargin96Candidate(conservative)) {
222
+ return false;
223
+ }
224
+ const fullPosition = fullStrength?.position ?? {};
225
+ const conservativePosition = conservative?.position ?? {};
226
+ return fullPosition.x === conservativePosition.x &&
227
+ fullPosition.y === conservativePosition.y &&
228
+ fullPosition.width === NEW_MARGIN_96_SIZE &&
229
+ fullPosition.height === NEW_MARGIN_96_SIZE &&
230
+ conservativePosition.width === NEW_MARGIN_96_SIZE &&
231
+ conservativePosition.height === NEW_MARGIN_96_SIZE;
232
+ }
233
+
234
+ export function shouldPreferFullStrengthNewMarginVariant(fullStrength, conservative) {
235
+ if (!hasSameExactNewMarginAnchor(fullStrength, conservative)) return false;
236
+ if (numberOr(fullStrength?.alphaGain) !== 1) return false;
237
+ if (numberOr(conservative?.alphaGain) >= 1) return false;
238
+ if (getProvenance(fullStrength).darkPolarity === true) return false;
239
+
240
+ const originalSpatial = numberOr(fullStrength?.originalSpatialScore, -Infinity);
241
+ const originalGradient = numberOr(fullStrength?.originalGradientScore, -Infinity);
242
+ const fullGradient = Math.max(0, numberOr(
243
+ fullStrength?.processedGradientScore,
244
+ Infinity
245
+ ));
246
+ const conservativeGradient = Math.max(0, numberOr(
247
+ conservative?.processedGradientScore,
248
+ Infinity
249
+ ));
250
+ const clippedRatio = numberOr(
251
+ fullStrength?.damage?.newlyClippedRatio,
252
+ Infinity
253
+ );
254
+ const provenance = getProvenance(fullStrength);
255
+ const positiveLocalizedSourceWitness =
256
+ provenance.sourceWitnessRescue === true &&
257
+ numberOr(
258
+ provenance.sourceWitnessGate?.spatialSignedTarget,
259
+ -Infinity
260
+ ) > 0;
261
+
262
+ // The full-strength trial scores itself with the same alpha profile it
263
+ // applies, so a negative spatial residual or noisy damage warning is not
264
+ // independent rejection evidence. Require positive source polarity and
265
+ // either a real gradient advantage or the decoy-localized source witness.
266
+ return originalSpatial > 0 &&
267
+ originalGradient >= FULL_STRENGTH_NEW_MARGIN_MIN_ORIGINAL_GRADIENT &&
268
+ fullGradient <= FULL_STRENGTH_NEW_MARGIN_MAX_PROCESSED_GRADIENT &&
269
+ (
270
+ positiveLocalizedSourceWitness ||
271
+ conservativeGradient - fullGradient >=
272
+ FULL_STRENGTH_NEW_MARGIN_MIN_GRADIENT_ADVANTAGE
273
+ ) &&
274
+ clippedRatio <= FULL_STRENGTH_NEW_MARGIN_MAX_CLIPPED_RATIO;
275
+ }
276
+
277
+ export function arbitrateCandidateByEvaluation(currentBest, candidate) {
278
+ if (shouldPreferFullStrengthNewMarginVariant(currentBest, candidate)) {
279
+ return currentBest;
280
+ }
281
+ if (shouldPreferFullStrengthNewMarginVariant(candidate, currentBest)) {
282
+ return candidate;
283
+ }
284
+ if (shouldPreferDefaultAlphaNewMarginCandidate(currentBest, candidate)) {
285
285
  return candidate;
286
286
  }
287
287
  if (shouldPreferDefaultAlphaNewMarginCandidate(candidate, currentBest)) {