unique-sdk 2026.50.0.dev13__tar.gz → 2026.50.0.dev14__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.
Files changed (96) hide show
  1. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/PKG-INFO +1 -1
  2. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/pyproject.toml +1 -1
  3. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/cli.py +97 -0
  4. unique_sdk-2026.50.0.dev14/unique_sdk/cli/commands/user_memory.py +163 -0
  5. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/README.md +0 -0
  6. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/__init__.py +0 -0
  7. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_api_requestor.py +0 -0
  8. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_api_resource.py +0 -0
  9. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_api_version.py +0 -0
  10. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_error.py +0 -0
  11. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_http_client.py +0 -0
  12. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_list_object.py +0 -0
  13. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_object_classes.py +0 -0
  14. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_request_options.py +0 -0
  15. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_unique_object.py +0 -0
  16. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_unique_ql.py +0 -0
  17. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_unique_response.py +0 -0
  18. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_util.py +0 -0
  19. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_version.py +0 -0
  20. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/_webhook.py +0 -0
  21. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/__init__.py +0 -0
  22. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_acronyms.py +0 -0
  23. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_agentic_table.py +0 -0
  24. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_analytics_order.py +0 -0
  25. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_benchmarking.py +0 -0
  26. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_briefing.py +0 -0
  27. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_chat_completion.py +0 -0
  28. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_content.py +0 -0
  29. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_context_memory.py +0 -0
  30. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_elicitation.py +0 -0
  31. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_embedding.py +0 -0
  32. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_event.py +0 -0
  33. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_folder.py +0 -0
  34. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_group.py +0 -0
  35. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_integrated.py +0 -0
  36. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_llm_models.py +0 -0
  37. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_mcp.py +0 -0
  38. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_message.py +0 -0
  39. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_message_assessment.py +0 -0
  40. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_message_execution.py +0 -0
  41. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_message_log.py +0 -0
  42. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_message_tool.py +0 -0
  43. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_module.py +0 -0
  44. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_project_chat.py +0 -0
  45. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_project_memory.py +0 -0
  46. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_scheduled_task.py +0 -0
  47. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_search.py +0 -0
  48. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_search_string.py +0 -0
  49. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_short_term_memory.py +0 -0
  50. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_space.py +0 -0
  51. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_user.py +0 -0
  52. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/api_resources/_web_search.py +0 -0
  53. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/__init__.py +0 -0
  54. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/__main__.py +0 -0
  55. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/__init__.py +0 -0
  56. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/_citation_manifest.py +0 -0
  57. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/agentic_table.py +0 -0
  58. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/agentic_table_write.py +0 -0
  59. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/browser.py +0 -0
  60. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/cite_file.py +0 -0
  61. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/elicitation.py +0 -0
  62. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/files.py +0 -0
  63. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/folders.py +0 -0
  64. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/mcp.py +0 -0
  65. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/navigation.py +0 -0
  66. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/read.py +0 -0
  67. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/scheduled_tasks.py +0 -0
  68. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/search.py +0 -0
  69. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/subagent.py +0 -0
  70. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/web_search.py +0 -0
  71. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/commands/web_search_config.py +0 -0
  72. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/config.py +0 -0
  73. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/formatting.py +0 -0
  74. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/identity.py +0 -0
  75. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/metadata_filter.py +0 -0
  76. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/paths.py +0 -0
  77. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/shell.py +0 -0
  78. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-agentic-table/SKILL.md +0 -0
  79. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-elicitation/SKILL.md +0 -0
  80. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-file-management/SKILL.md +0 -0
  81. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-mcp/SKILL.md +0 -0
  82. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-scheduled-tasks/SKILL.md +0 -0
  83. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-search/SKILL.md +0 -0
  84. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-subagent/SKILL.md +0 -0
  85. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-uploaded-search/SKILL.md +0 -0
  86. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/skills/unique-cli-web-search/SKILL.md +0 -0
  87. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/state.py +0 -0
  88. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/subagent_turns.py +0 -0
  89. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/cli/workspace.py +0 -0
  90. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/utils/analytics_order_run.py +0 -0
  91. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/utils/benchmarking_run.py +0 -0
  92. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/utils/chat_history.py +0 -0
  93. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/utils/chat_in_space.py +0 -0
  94. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/utils/file_io.py +0 -0
  95. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/utils/sources.py +0 -0
  96. {unique_sdk-2026.50.0.dev13 → unique_sdk-2026.50.0.dev14}/unique_sdk/utils/token.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: unique-sdk
3
- Version: 2026.50.0.dev13
3
+ Version: 2026.50.0.dev14
4
4
  Summary:
5
5
  Author: Martin Fadler, Konstantin Krauss, Andreas Hauri
6
6
  Author-email: Martin Fadler <martin.fadler@unique.ch>, Konstantin Krauss <konstantin@unique.ch>, Andreas Hauri <andreas@unique.ch>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "unique_sdk"
3
- version = "2026.50.0.dev13"
3
+ version = "2026.50.0.dev14"
4
4
  description = ""
5
5
  readme = "README.md"
6
6
  license = { text = "MIT" }
@@ -110,6 +110,17 @@ from unique_sdk.cli.commands.subagent import cmd_subagent
110
110
  from unique_sdk.cli.commands.subagent import (
111
111
  is_error_output as _is_subagent_error_output,
112
112
  )
113
+ from unique_sdk.cli.commands.user_memory import (
114
+ MAX_STDIN_BYTES,
115
+ MEMORY_ERROR_PREFIX,
116
+ access_denied_output,
117
+ cmd_memory_read,
118
+ cmd_memory_write,
119
+ memory_access_enabled,
120
+ )
121
+ from unique_sdk.cli.commands.user_memory import (
122
+ is_error_output as _is_memory_error_output,
123
+ )
113
124
  from unique_sdk.cli.commands.web_search import (
114
125
  QUERY_REVIEW_WAIT_TIMEOUT_SECONDS,
115
126
  cmd_web_crawl,
@@ -229,6 +240,7 @@ Examples:
229
240
  unique-cli versions cont_abc123 List archived file versions
230
241
  unique-cli restore-version cver_1 Restore a file from a version
231
242
  unique-cli elicit ask "Which?" Ask the user a question synchronously
243
+ unique-cli memory read Show your memory profile and its version
232
244
  unique-cli subagent Legal "Review" Invoke a connected space/subagent
233
245
  unique-cli web-search search "x" Search the web via the public API
234
246
  unique-cli web-search crawl URL Crawl a URL via the public API
@@ -1295,6 +1307,91 @@ def schedule_delete(ctx: click.Context, task_id: str) -> None:
1295
1307
  ctx.exit(1)
1296
1308
 
1297
1309
 
1310
+ # -- User memory ----------------------------------------------------------
1311
+
1312
+
1313
+ @main.group()
1314
+ def memory() -> None:
1315
+ """Read or change the signed-in user's memory profile.
1316
+
1317
+ \b
1318
+ The profile is stored per user, not in a knowledge-base folder, so a
1319
+ space's knowledge scope does not limit it. Both commands work only
1320
+ when the platform sets $UNIQUE_USER_MEMORY_ACCESS=1 for the turn.
1321
+
1322
+ \b
1323
+ Subcommands:
1324
+ read Print the profile body and its version
1325
+ write Replace the profile body with the text on stdin
1326
+ """
1327
+
1328
+
1329
+ @memory.command(name="read")
1330
+ @click.pass_context
1331
+ def memory_read(ctx: click.Context) -> None:
1332
+ """Print the memory profile body, preceded by a `version:` line.
1333
+
1334
+ \b
1335
+ Pass that version to `unique-cli memory write --expect-version`.
1336
+ A user without a profile reads `version: none`.
1337
+
1338
+ \b
1339
+ Examples:
1340
+ unique-cli memory read
1341
+ """
1342
+ if not memory_access_enabled():
1343
+ emit(access_denied_output(), is_error=_is_memory_error_output)
1344
+ return
1345
+ emit(cmd_memory_read(LazyState.get(ctx)), is_error=_is_memory_error_output)
1346
+
1347
+
1348
+ @memory.command(name="write")
1349
+ @click.option(
1350
+ "--expect-version",
1351
+ required=True,
1352
+ help="The version `unique-cli memory read` printed. The write is "
1353
+ "refused when the stored profile changed since then.",
1354
+ )
1355
+ @click.pass_context
1356
+ def memory_write(ctx: click.Context, expect_version: str) -> None:
1357
+ """Replace the memory profile body with the text on stdin.
1358
+
1359
+ \b
1360
+ Pass the complete profile body, starting with its first `## ` section.
1361
+ Frontmatter on stdin is ignored; the platform writes its own. The
1362
+ text is never written to disk. stdin may hold at most 272 KiB, and
1363
+ the body after frontmatter at most 256 KiB.
1364
+
1365
+ \b
1366
+ Examples:
1367
+ unique-cli memory write --expect-version sha256:3f1c9a0b7d2e4f68 <<'EOF'
1368
+ ## Identity
1369
+ - Employer: Unique AG
1370
+ EOF
1371
+ """
1372
+ if not memory_access_enabled():
1373
+ emit(access_denied_output(), is_error=_is_memory_error_output)
1374
+ return
1375
+ raw = click.get_binary_stream("stdin").read(MAX_STDIN_BYTES + 1)
1376
+ if len(raw) > MAX_STDIN_BYTES:
1377
+ emit(
1378
+ f"{MEMORY_ERROR_PREFIX} input is larger than {MAX_STDIN_BYTES} "
1379
+ "bytes; nothing was saved.",
1380
+ is_error=_is_memory_error_output,
1381
+ )
1382
+ return
1383
+ try:
1384
+ text = raw.decode("utf-8")
1385
+ except UnicodeDecodeError:
1386
+ emit(
1387
+ f"{MEMORY_ERROR_PREFIX} input is not valid UTF-8; nothing was saved.",
1388
+ is_error=_is_memory_error_output,
1389
+ )
1390
+ return
1391
+ output = cmd_memory_write(LazyState.get(ctx), text, expect_version=expect_version)
1392
+ emit(output, is_error=_is_memory_error_output)
1393
+
1394
+
1298
1395
  # -- Elicitations ----------------------------------------------------------
1299
1396
 
1300
1397
 
@@ -0,0 +1,163 @@
1
+ """Read and write the signed-in user's memory profile through the context-memory API.
2
+
3
+ The profile is addressed by user and company, never by a knowledge-base
4
+ folder, so a space's folder scope does not apply to it. Text moves through
5
+ stdin and stdout and is never written to disk.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import hashlib
11
+ import os
12
+ import re
13
+ from datetime import datetime, timezone
14
+
15
+ import unique_sdk
16
+ from unique_sdk.cli.state import ShellState
17
+
18
+ MEMORY_ERROR_PREFIX = "memory:"
19
+ MEMORY_ACCESS_ENV = "UNIQUE_USER_MEMORY_ACCESS"
20
+ MAX_PROFILE_BYTES = 256 * 1024
21
+ # The whole of stdin is bounded too. The 16 KiB on top of the body limit is
22
+ # room for frontmatter, which is dropped before the body limit applies.
23
+ MAX_STDIN_BYTES = MAX_PROFILE_BYTES + 16 * 1024
24
+ NO_PROFILE_VERSION = "none"
25
+
26
+ # The empty case comes first so `---\n---` never scans ahead to a later
27
+ # `---` horizontal rule in the body.
28
+ _FRONTMATTER_RE = re.compile(r"^---\n(?:---|.*?\n---)[ \t]*(?:\n|\Z)", re.DOTALL)
29
+ _TURN_COUNT_RE = re.compile(r"^turn_count:\s*(\d+)\s*$", re.MULTILINE)
30
+
31
+
32
+ class _StoredProfile:
33
+ __slots__ = ("document", "version")
34
+
35
+ def __init__(self, *, document: str, version: str) -> None:
36
+ self.document = document
37
+ self.version = version
38
+
39
+
40
+ def is_error_output(output: str) -> bool:
41
+ """Return True if `output` is a memory command error message."""
42
+ return output.startswith(MEMORY_ERROR_PREFIX)
43
+
44
+
45
+ def memory_access_enabled() -> bool:
46
+ """True when the platform turned memory access on for this turn."""
47
+ return os.environ.get(MEMORY_ACCESS_ENV, "") == "1"
48
+
49
+
50
+ def access_denied_output() -> str:
51
+ return (
52
+ f"{MEMORY_ERROR_PREFIX} permission denied: user memory cannot be "
53
+ "read or changed in this space."
54
+ )
55
+
56
+
57
+ def profile_body(text: str) -> str:
58
+ """The profile without its YAML frontmatter, with LF line endings."""
59
+ normalized = text.replace("\r\n", "\n")
60
+ return _FRONTMATTER_RE.sub("", normalized, count=1).strip()
61
+
62
+
63
+ def stored_profile(*, body: str, previous: str, user_id: str) -> str:
64
+ """The stored document: platform frontmatter plus the given body."""
65
+ timestamp = datetime.now(timezone.utc).isoformat(timespec="seconds")
66
+ return (
67
+ "---\n"
68
+ f"user_id: {user_id}\n"
69
+ "schema_version: 1\n"
70
+ f"last_updated: {timestamp}\n"
71
+ f"turn_count: {_turn_count(previous)}\n"
72
+ "---\n\n"
73
+ f"{body}\n"
74
+ )
75
+
76
+
77
+ def _turn_count(profile: str) -> int:
78
+ frontmatter = _FRONTMATTER_RE.match(profile)
79
+ if frontmatter is None:
80
+ return 0
81
+ match = _TURN_COUNT_RE.search(frontmatter.group(0))
82
+ return int(match.group(1)) if match else 0
83
+
84
+
85
+ def _load(state: ShellState) -> _StoredProfile:
86
+ try:
87
+ current = unique_sdk.ContextMemory.retrieve(
88
+ user_id=state.config.user_id,
89
+ company_id=state.config.company_id,
90
+ )
91
+ except unique_sdk.UniqueError as e:
92
+ if e.http_status == 404:
93
+ return _StoredProfile(document="", version=NO_PROFILE_VERSION)
94
+ raise
95
+ document = getattr(current, "document", "") or ""
96
+ if not profile_body(document):
97
+ return _StoredProfile(document="", version=NO_PROFILE_VERSION)
98
+ return _StoredProfile(document=document, version=content_version(document))
99
+
100
+
101
+ def content_version(document: str) -> str:
102
+ """The version of a stored document.
103
+
104
+ A hash of the content, not the API's `updatedAt`: the read that creates a
105
+ user's first profile reports a different `updatedAt` than every later read
106
+ of the same document, which would refuse that user's first save.
107
+ """
108
+ digest = hashlib.sha256(document.encode("utf-8")).hexdigest()
109
+ return f"sha256:{digest[:16]}"
110
+
111
+
112
+ def cmd_memory_read(state: ShellState) -> str:
113
+ """The stored profile body, preceded by its version line."""
114
+ if not memory_access_enabled():
115
+ return access_denied_output()
116
+ try:
117
+ stored = _load(state)
118
+ except unique_sdk.UniqueError as e:
119
+ return f"{MEMORY_ERROR_PREFIX} read failed: {e}"
120
+ body = profile_body(stored.document)
121
+ if not body:
122
+ return f"version: {NO_PROFILE_VERSION}\n\n(no memory profile yet)"
123
+ return f"version: {stored.version}\n\n{body}"
124
+
125
+
126
+ def cmd_memory_write(state: ShellState, text: str, *, expect_version: str) -> str:
127
+ """Replace the profile body with `text` if the stored version still matches."""
128
+ if not memory_access_enabled():
129
+ return access_denied_output()
130
+ body = profile_body(text)
131
+ if not body:
132
+ return f"{MEMORY_ERROR_PREFIX} empty profile on stdin; nothing was saved."
133
+ if len(body.encode("utf-8")) > MAX_PROFILE_BYTES:
134
+ return (
135
+ f"{MEMORY_ERROR_PREFIX} profile is larger than "
136
+ f"{MAX_PROFILE_BYTES} bytes; nothing was saved."
137
+ )
138
+ if not body.startswith("## "):
139
+ return (
140
+ f"{MEMORY_ERROR_PREFIX} the profile must start with its first "
141
+ "`## ` section heading; nothing was saved."
142
+ )
143
+ try:
144
+ stored = _load(state)
145
+ if stored.version != expect_version.strip():
146
+ return (
147
+ f"{MEMORY_ERROR_PREFIX} the profile changed since it was read "
148
+ f"(now version {stored.version}). Run `unique-cli memory read` "
149
+ "again and reapply the change; nothing was saved."
150
+ )
151
+ if profile_body(stored.document) == body:
152
+ return "Memory unchanged; nothing was saved."
153
+ document = stored_profile(
154
+ body=body, previous=stored.document, user_id=state.config.user_id
155
+ )
156
+ _ = unique_sdk.ContextMemory.modify(
157
+ user_id=state.config.user_id,
158
+ company_id=state.config.company_id,
159
+ document=document,
160
+ )
161
+ except unique_sdk.UniqueError as e:
162
+ return f"{MEMORY_ERROR_PREFIX} save failed: {e}"
163
+ return f"Memory saved ({len(document.encode('utf-8'))} bytes)."