unique-user-memory 2026.50.0.dev0__tar.gz → 2026.50.0.dev1__tar.gz

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.
@@ -1,14 +1,14 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: unique-user-memory
3
- Version: 2026.50.0.dev0
3
+ Version: 2026.50.0.dev1
4
4
  Summary:
5
5
  Author: Fabian Schläpfer
6
6
  Author-email: Fabian Schläpfer <fabian@unique.ch>
7
7
  License: Proprietary
8
8
  Requires-Dist: jinja2>=3.1.6
9
9
  Requires-Dist: pydantic>=2.8.2
10
- Requires-Dist: unique-sdk>=2026.50.0.dev0,<2026.50.0rc0
11
- Requires-Dist: unique-toolkit[monitoring]>=2026.50.0.dev0,<2026.50.0rc0
10
+ Requires-Dist: unique-sdk>=2026.50.0.dev13,<2026.50.0rc0
11
+ Requires-Dist: unique-toolkit[monitoring]>=2026.50.0.dev3,<2026.50.0rc0
12
12
  Requires-Python: >=3.12, <4
13
13
  Description-Content-Type: text/markdown
14
14
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "unique_user_memory"
3
- version = "2026.50.0.dev0"
3
+ version = "2026.50.0.dev1"
4
4
  description = ""
5
5
  authors = [
6
6
  { name = "Fabian Schläpfer", email = "fabian@unique.ch" },
@@ -11,8 +11,8 @@ requires-python = ">=3.12,<4"
11
11
  dependencies = [
12
12
  "jinja2>=3.1.6",
13
13
  "pydantic>=2.8.2",
14
- "unique-sdk>=2026.50.0.dev0,<2026.50.0rc0",
15
- "unique-toolkit[monitoring]>=2026.50.0.dev0,<2026.50.0rc0",
14
+ "unique-sdk>=2026.50.0.dev13,<2026.50.0rc0",
15
+ "unique-toolkit[monitoring]>=2026.50.0.dev3,<2026.50.0rc0",
16
16
  ]
17
17
 
18
18
  [dependency-groups]
@@ -24,15 +24,16 @@ from unique_toolkit.language_model.schemas import (
24
24
 
25
25
  from unique_user_memory.config import UserMemoryConfig
26
26
  from unique_user_memory.user_memory import (
27
+ _DEFAULT_LANGUAGE_MODEL,
27
28
  UserMemoryState,
28
29
  _gate_max_tokens,
30
+ _hard_cut,
29
31
  _is_well_formed_profile,
30
32
  _sanitize_for_xml_context,
31
33
  condense_user_memory,
32
34
  consolidate_user_memory,
33
35
  count_tokens,
34
36
  download_user_memory,
35
- enforce_token_cap,
36
37
  ensure_user_memory_folder,
37
38
  fit_user_memory,
38
39
  profile_body,
@@ -216,22 +217,26 @@ def test_profile_body_returns_empty_for_frontmatter_only_profile() -> None:
216
217
  assert profile_body("---\nuser_id: 42\nturn_count: 0\n---\n") == ""
217
218
 
218
219
 
219
- def test_enforce_token_cap_truncates_long_content() -> None:
220
+ def test_hard_cut_truncates_long_content() -> None:
220
221
  content = "\n\n".join(f"paragraph {index} " + "word " * 40 for index in range(50))
221
222
 
222
- capped = enforce_token_cap(content=content, max_tokens=120)
223
+ capped = _hard_cut(
224
+ content=content, max_tokens=120, language_model=_DEFAULT_LANGUAGE_MODEL
225
+ )
223
226
 
224
227
  assert "<!-- truncated to fit memory budget -->" in capped
225
228
  assert len(capped) < len(content)
226
229
 
227
230
 
228
- def test_enforce_token_cap_keeps_body_when_section_exceeds_budget() -> None:
231
+ def test_hard_cut_keeps_body_when_section_exceeds_budget() -> None:
229
232
  # A single section whose bullets are joined by single newlines used to be
230
233
  # treated as one indivisible paragraph, dropping the whole body.
231
234
  bullets = "\n".join(f"- fact number {index} about the user" for index in range(200))
232
235
  content = f"# User Memory\n\n## Identity\n{bullets}"
233
236
 
234
- capped = enforce_token_cap(content=content, max_tokens=120)
237
+ capped = _hard_cut(
238
+ content=content, max_tokens=120, language_model=_DEFAULT_LANGUAGE_MODEL
239
+ )
235
240
 
236
241
  assert "<!-- truncated to fit memory budget -->" in capped
237
242
  assert "# User Memory" in capped
@@ -241,6 +246,28 @@ def test_enforce_token_cap_keeps_body_when_section_exceeds_budget() -> None:
241
246
  assert count_tokens(content=capped) <= 120
242
247
 
243
248
 
249
+ @pytest.mark.asyncio
250
+ async def test_stored_profile_cut_leaves_no_broken_character() -> None:
251
+ # One long line of emoji forces a token-level cut; at this budget the
252
+ # old cut split a character and left U+FFFD in the profile (UN-30836).
253
+ profile = "🧠🚀🎯📊💡🔥✅🧩" * 600
254
+
255
+ result = await consolidate_user_memory(
256
+ current_memory=profile,
257
+ user_id="u1",
258
+ user_message="",
259
+ assistant_message="",
260
+ config=UserMemoryConfig(max_tokens=502),
261
+ language_model=_DEFAULT_LANGUAGE_MODEL,
262
+ event=MagicMock(),
263
+ logger=MagicMock(),
264
+ )
265
+
266
+ assert "\ufffd" not in result
267
+ assert result.endswith("<!-- truncated to fit memory budget -->")
268
+ assert count_tokens(content=result) <= 502
269
+
270
+
244
271
  @pytest.mark.asyncio
245
272
  async def test_fit_user_memory_returns_unchanged_when_within_budget() -> None:
246
273
  content = "# User Memory\n\n## Identity\n- short"
@@ -5,6 +5,8 @@ from datetime import datetime, timezone
5
5
  from logging import Logger
6
6
 
7
7
  import unique_sdk
8
+ from unique_toolkit._common.token import count_tokens as toolkit_count_tokens
9
+ from unique_toolkit._common.token import truncate_to_token_limit
8
10
  from unique_toolkit.app import ChatEvent
9
11
  from unique_toolkit.content.functions import (
10
12
  download_content_to_bytes_async,
@@ -17,8 +19,6 @@ from unique_toolkit.language_model import (
17
19
  LanguageModelService,
18
20
  LanguageModelSystemMessage,
19
21
  LanguageModelUserMessage,
20
- TypeDecoder,
21
- TypeEncoder,
22
22
  )
23
23
  from unique_toolkit.language_model.infos import (
24
24
  LanguageModelInfo,
@@ -151,79 +151,21 @@ def _gate_max_tokens(*, language_model: LanguageModelInfo) -> int:
151
151
  return _GATE_MAX_TOKENS
152
152
 
153
153
 
154
- def _get_model_tokenizer(
155
- *,
156
- language_model: LanguageModelInfo,
157
- ) -> tuple[TypeEncoder, TypeDecoder]:
158
- model_info = language_model or _DEFAULT_LANGUAGE_MODEL
159
- return model_info.get_encoder(), model_info.get_decoder()
160
-
161
-
162
- def _count_tokens(
163
- *,
164
- content: str,
165
- encode: TypeEncoder,
166
- ) -> int:
167
- if not content:
168
- return 0
169
- return len(encode(content))
170
-
171
-
172
154
  def count_tokens(
173
155
  *,
174
156
  content: str,
175
157
  language_model: LanguageModelInfo = _DEFAULT_LANGUAGE_MODEL,
176
158
  ) -> int:
177
- encode, _ = _get_model_tokenizer(language_model=language_model)
178
- return _count_tokens(content=content, encode=encode)
159
+ return toolkit_count_tokens(content, language_model or _DEFAULT_LANGUAGE_MODEL)
179
160
 
180
161
 
181
- def enforce_token_cap(
182
- *,
183
- content: str,
184
- max_tokens: int,
185
- language_model: LanguageModelInfo = _DEFAULT_LANGUAGE_MODEL,
162
+ def _hard_cut(
163
+ *, content: str, max_tokens: int, language_model: LanguageModelInfo
186
164
  ) -> str:
187
- if not content:
188
- return content
189
-
190
- encode, decode = _get_model_tokenizer(language_model=language_model)
191
-
192
- if _count_tokens(content=content, encode=encode) <= max_tokens:
193
- return content
194
-
195
- marker_tokens = _count_tokens(content=_TRUNCATION_MARKER, encode=encode)
196
- target = max(0, max_tokens - marker_tokens)
197
- # Split on lines rather than blank-line paragraphs: memory profiles keep
198
- # each bullet on its own line inside a section, so paragraph-level cuts
199
- # would treat a whole (multi-thousand-token) section as one indivisible
200
- # unit and drop the entire body once it exceeds the budget.
201
- lines = content.split("\n")
202
- accepted: list[str] = []
203
- running = 0
204
- for line in lines:
205
- line_tokens = _count_tokens(content=line + "\n", encode=encode)
206
- if running + line_tokens > target:
207
- break
208
- accepted.append(line)
209
- running += line_tokens
210
-
211
- if accepted:
212
- truncated = "\n".join(accepted)
213
- else:
214
- truncated = decode(encode(content)[:target])
215
-
216
- result = truncated.rstrip() + _TRUNCATION_MARKER
217
- shrink = target
218
- while _count_tokens(content=result, encode=encode) > max_tokens and shrink > 0:
219
- shrink -= 1
220
- body = decode(encode(result)[:shrink]).rstrip()
221
- result = body + _TRUNCATION_MARKER
222
-
223
- if _count_tokens(content=result, encode=encode) > max_tokens:
224
- result = decode(encode(result)[:max_tokens])
225
-
226
- return result
165
+ """Cut ``content`` to ``max_tokens`` at a line, with user memory's marker."""
166
+ return truncate_to_token_limit(
167
+ content, max_tokens, language_model, marker=_TRUNCATION_MARKER
168
+ )
227
169
 
228
170
 
229
171
  async def condense_user_memory(
@@ -346,7 +288,7 @@ async def fit_user_memory(
346
288
 
347
289
  Fast path: content already within budget is returned untouched (no LLM
348
290
  call). Otherwise the profile is first condensed by the LLM, and only a
349
- still-oversized result is hard-cut by :func:`enforce_token_cap`.
291
+ still-oversized result is hard-cut at a line boundary.
350
292
  """
351
293
  if not content:
352
294
  return content
@@ -395,7 +337,7 @@ async def fit_user_memory(
395
337
  else:
396
338
  record_condense(trigger, "llm_failed_then_hard_cut")
397
339
 
398
- result = enforce_token_cap(
340
+ result = _hard_cut(
399
341
  content=content,
400
342
  max_tokens=max_tokens,
401
343
  language_model=language_model,
@@ -880,7 +822,7 @@ async def consolidate_user_memory(
880
822
  it finishes (even on failure), so a caller can surface a transient
881
823
  "updating memory" notice to the user only while real work happens.
882
824
  """
883
- safe_current = enforce_token_cap(
825
+ safe_current = _hard_cut(
884
826
  content=current_memory,
885
827
  max_tokens=config.max_tokens,
886
828
  language_model=language_model,
@@ -894,7 +836,7 @@ async def consolidate_user_memory(
894
836
  )
895
837
 
896
838
  if not (user_message or "").strip() and not (assistant_message or "").strip():
897
- return safe_current or enforce_token_cap(
839
+ return safe_current or _hard_cut(
898
840
  content=empty_profile(user_id),
899
841
  max_tokens=config.max_tokens,
900
842
  language_model=language_model,