Spaces:
Running on CPU Upgrade

Vers3Dynamics commited on
Commit
628eabb
·
1 Parent(s): 615fec3

Keep full protected sentences intact in rewrites

Browse files
humantouch/engine.py CHANGED
@@ -23,6 +23,7 @@ from .intent import DEFAULT_AVOID, GOALS, PRESERVE_LABELS, STRENGTH_PRESERVE, fi
23
  from .invariants import extract_invariants
24
  from .ledger import (
25
  apply_segments,
 
26
  build_changes,
27
  capitalization_dependents,
28
  changes_overlapping,
@@ -292,7 +293,10 @@ class HumanTouch:
292
  spans.append((sentences[0].start, sentences[0].end))
293
  if sentences and 'ending' in intent.preserve:
294
  spans.append((sentences[-1].start, sentences[-1].end))
295
- for change_id in sorted(changes_overlapping(segments, spans)):
 
 
 
296
  restored[change_id] = 'protected text'
297
  if spans and restored:
298
  actions.append(f'{len(restored)} edit{"s were" if len(restored) != 1 else " was"} not applied because '
 
23
  from .invariants import extract_invariants
24
  from .ledger import (
25
  apply_segments,
26
+ attached_sentence_prefixes,
27
  build_changes,
28
  capitalization_dependents,
29
  changes_overlapping,
 
293
  spans.append((sentences[0].start, sentences[0].end))
294
  if sentences and 'ending' in intent.preserve:
295
  spans.append((sentences[-1].start, sentences[-1].end))
296
+ protected_sentences = {s.start: s.text for s in sentences
297
+ if any(start <= s.start and s.end <= end for start, end in spans)}
298
+ protected_ids = changes_overlapping(segments, spans) | attached_sentence_prefixes(segments, protected_sentences)
299
+ for change_id in sorted(protected_ids):
300
  restored[change_id] = 'protected text'
301
  if spans and restored:
302
  actions.append(f'{len(restored)} edit{"s were" if len(restored) != 1 else " was"} not applied because '
humantouch/ledger.py CHANGED
@@ -176,6 +176,23 @@ def changes_overlapping(segments: list[Segment], spans: list[tuple[int, int]]) -
176
  hit.add(seg.change_id)
177
  position = end
178
  return hit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
 
181
  def capitalization_dependents(segments: list[Segment], restored: set[int]) -> set[int]:
 
176
  hit.add(seg.change_id)
177
  position = end
178
  return hit
179
+
180
+
181
+ def attached_sentence_prefixes(segments: list[Segment], sentences: dict[int, str]) -> set[int]:
182
+ """Find insertions that become part of a protected sentence at its left boundary.
183
+
184
+ Ledger overlap alone misses these zero-width edits. A separate sentence or
185
+ line before the protected one does not alter it and stays in the revision.
186
+ """
187
+ hits: set[int] = set()
188
+ position = 0
189
+ for seg in segments:
190
+ if not seg.a and seg.change_id is not None and (sentence := sentences.get(position)):
191
+ combined = seg.b + sentence
192
+ if not any(span.start == len(seg.b) and span.end == len(combined) for span in split_sentences(combined)):
193
+ hits.add(seg.change_id)
194
+ position += len(seg.a)
195
+ return hits
196
 
197
 
198
  def capitalization_dependents(segments: list[Segment], restored: set[int]) -> set[int]:
tests/test_preservation_regressions.py CHANGED
@@ -134,3 +134,36 @@ def test_guard_does_not_recase_intentional_lowercase_source(scripted):
134
  out = app.restore_voice(original, reference='A short note is enough. I leave it there.')
135
  assert out.result == original
136
  assert not any('original opening' in note for note in out.guard_actions)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  out = app.restore_voice(original, reference='A short note is enough. I leave it there.')
135
  assert out.result == original
136
  assert not any('original opening' in note for note in out.guard_actions)
137
+
138
+
139
+ @pytest.mark.parametrize('preserve', ['A small thing, but mine.', 'opening'])
140
+ def test_mcp_rewrite_protects_the_whole_sentence_and_keeps_the_requested_edit(scripted, preserve):
141
+ from mcp_surface import build_tools
142
+
143
+ original = 'A small thing, but mine. I left the prints on on the counter. I can bring a spare on Friday if that helps.'
144
+ model_output = 'It is a small thing, but it belongs to me. I left the prints on the counter. I will bring a spare on Friday if that helps.'
145
+ app, _ = scripted(model_output)
146
+ result = build_tools(app)['rewrite_in_voice'](
147
+ original, intent='Remove only the duplicate on in the second sentence.', preserve=preserve)
148
+ assert result['result'] == original.replace('on on', 'on')
149
+ assert any(c['revised'] == 'It is' and c['restored_by'] == 'protected text' for c in result['changes'])
150
+ assert any(c['original'] == 'on' and not c['restored'] for c in result['changes'])
151
+
152
+
153
+ @pytest.mark.parametrize(('original', 'proposed', 'preserve', 'expected'), [
154
+ ('A small thing, but mine. The room was quiet.', 'A small thing, but mine. The room was quiet, for once.',
155
+ 'ending', 'A small thing, but mine. The room was quiet.'),
156
+ ('A small thing, but mine. The room was quiet.', 'For context. A small thing, but mine. The room was quiet.',
157
+ 'opening', 'For context. A small thing, but mine. The room was quiet.'),
158
+ ('A small thing, but mine. The room was quiet.', 'It is a small thing, but mine. The room was quiet.',
159
+ 'small thing', 'It is a small thing, but mine. The room was quiet.'),
160
+ ('We waited. A small thing, but mine. The room was quiet.', 'We waited. It is a small thing, but mine. The room felt quiet.',
161
+ 'A small thing, but mine.', 'We waited. A small thing, but mine. The room felt quiet.'),
162
+ ('A short opening. the last note', 'A short opening. the last note for now',
163
+ 'ending', 'A short opening. the last note'),
164
+ ])
165
+ def test_mcp_protection_respects_sentence_boundaries(scripted, original, proposed, preserve, expected):
166
+ from mcp_surface import build_tools
167
+
168
+ app, _ = scripted(proposed)
169
+ assert build_tools(app)['rewrite_in_voice'](original, preserve=preserve)['result'] == expected