File size: 10,070 Bytes
1ad1eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
043da5c
 
1ad1eea
 
043da5c
1ad1eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Run authorized Assistant writers on a private store, then commit only their held documents.

The ordinary writer remains the permission/field/BU validator. Revalidation uses the held
execution clock and ids, and must reproduce the exact documents before any real write.
"""
import copy
import time
import uuid

from deps import Session
from core import write_execution


def _digest(value):
    from routes_external import _json_digest
    return _json_digest(value)


def _changes(before, after, path=()):
    """Exact store deltas, including absent keys and list removal rather than null cells."""
    if before == after:
        return []
    if isinstance(before, dict) and isinstance(after, dict):
        changes = []
        for key in sorted(before.keys() | after.keys()):
            at = [*path, key]
            if key not in after:
                changes.append({"path": at, "remove": True})
            elif key not in before:
                changes.append({"path": at, "after": copy.deepcopy(after[key])})
            else:
                changes.extend(_changes(before[key], after[key], at))
        return changes
    if isinstance(before, list) and isinstance(after, list):
        changes = []
        for i in range(min(len(before), len(after))):
            changes.extend(_changes(before[i], after[i], [*path, i]))
        changes.extend({"path": [*path, i], "after": copy.deepcopy(after[i])}
                       for i in range(len(before), len(after)))
        changes.extend({"path": [*path, i], "remove": True}
                       for i in range(len(before) - 1, len(after) - 1, -1))
        return changes
    return [{"path": list(path), "after": copy.deepcopy(after)}]


def project(current, document):
    if _digest(current) != document["before_digest"]:
        raise ValueError("The data changed after preview. Ask again to preview the change.")
    value = copy.deepcopy(current)
    for op in document["patch"]:
        path = op["path"]
        if not path:
            value = copy.deepcopy(op["after"])
            continue
        parent = value
        for key in path[:-1]:
            parent = parent[key]
        key = path[-1]
        if op.get("remove"):
            del parent[key]
        elif isinstance(parent, list) and key == len(parent):
            parent.append(copy.deepcopy(op["after"]))
        else:
            parent[key] = copy.deepcopy(op["after"])
    if _digest(value) != document["after_digest"]:
        raise ValueError("The held change could not be verified. Preview the change again.")
    return value


def prepare(session, name, args, *, prior=(), execution=None):
    import agent_runtime
    import routes_external as external

    runtime = external._PreviewRuntime(session.runtime)
    runtime.preview_only = True
    runtime.deferred_notices = []
    runtime.deferred_audits = []
    for step in prior:
        for key, document in step["preview"]["documents"].items():
            runtime.put(key, project(runtime.get(key), document))
    # Prior held steps are the starting point for this step, not part of its diff.
    runtime._initial = copy.deepcopy(runtime._docs)
    actor = Session(session.tenant, copy.deepcopy(session.user), copy.deepcopy(session.claims), runtime)
    execution = execution or {"seed": uuid.uuid4().hex, "timestamp": time.time()}
    with write_execution.replay(execution):
        result = agent_runtime._dispatch(actor, name, copy.deepcopy(args))
    if isinstance(result, dict) and (result.get("ok") is False or result.get("refused")):
        raise ValueError(agent_runtime._refusal(result))
    documents = runtime.changed()
    diff, truncated = external._diff_documents(documents)
    if truncated:
        raise ValueError("The complete change is too large to preview. Split it into smaller changes.")
    # A valid no-op still goes through the writer and may have a meaningful return value.
    preview = {"execution": execution, "documents": {
                   key: {"before_digest": _digest(pair["before"]), "after_digest": _digest(pair["after"]),
                         "patch": _changes(pair["before"], pair["after"])}
                   for key, pair in documents.items()}, "result": result,
               "reads": {key: _digest(value) for key, value in runtime._initial.items()}, "diff": diff,
               "notices": runtime.deferred_notices, "audits": runtime.deferred_audits}
    external._canonical_json(preview)
    return preview


def lines(preview, name, args, session):
    import agent_runtime
    import json
    result = preview["result"]
    heading = agent_runtime._change_lines(name, args, session)
    if isinstance(result, dict):
        if result.get("label") and (name.startswith("schedule_") or name == "reminder_create"):
            return [f"{name.replace('_', ' ').capitalize()}: {result['label']}.",
                    result.get("message") or f"{result['inWords']} ({result['timezone']})."]
        elif name.startswith("ticket_") and result.get("title"):
            detail = ("Assigned to: " + result["assigned_to"] if name == "ticket_assign" else
                      "Snoozed until: " + result["snoozed_until"] if name == "ticket_snooze" else
                      "Status: " + result["status"])
            return [f"{name.replace('_', ' ').capitalize()}: {result['title']}.", detail]
        elif name == "remember":
            from assistant_memory import _text
            return ["Remember: " + _text(args.get("text")), "Scope: " + str(result.get("scope") or "company")]
        elif name == "tool_save":
            saved = result["saved"]
            return ["Save tool " + saved["title"] + " for you.",
                    *(step["tool"].replace("_", " ") + ": " + json.dumps(step["args"], ensure_ascii=True)
                      for step in saved["steps"])]
        elif name == "tool_share":
            return ["Share tool " + args["name"] + ".", result.get("message") or "Share with the workspace."]
        elif name == "tool_share_decide":
            return [("Share " + result["tool"]["title"] + " with the workspace."
                     if result.get("shared") else "Decline the tool sharing request.")]
        elif name == "agent_message":
            return ["Send " + args["type"].replace("_", " ") + " to " + str(args.get("to") or "the item owner") + ".",
                    str(result.get("text") or args.get("text") or "")]
        elif name == "open_items":
            return str(result["message"]).splitlines()
    # The held delta contains every normalized change. Show values relevant to the decision;
    # revisions, attribution and store addresses do not help a person approve a change.
    metadata = {"id", "rev", "schemaVersion", "created_at", "updated_at", "createdAt", "updatedAt",
                "created_by", "updatedBy", "writes", "writesFrom", "refreshed_at", "has_access"}
    details = []
    def show(value):
        if isinstance(value, dict):
            if value.get("kind") == "literal":
                return value.get("value")
            return {k: show(v) for k, v in value.items() if k not in metadata}
        return [show(v) for v in value] if isinstance(value, list) else value
    for row in preview["diff"]:
        path = row["path"].split("/")[1:]
        if not path or path[-1] in metadata or path[0] == "sheet_workbooks" or "writes" in path:
            continue
        if name in {"sheet_create_workbook", "sheet_add_tab", "sheet_delete_tab", "view_delete", "query_view_delete", "app_delete"}:
            continue
        label = path[-1].replace("_", " ").capitalize()
        # A container name is a record or cell address only when an id follows it.
        if "cells" in path[:-1]:
            index = path.index("cells")
            label = "Cell " + path[index + 1] + (" " + path[-1] if len(path) > index + 2 else "")
        elif "rows" in path[:-1]:
            index = path.index("rows")
            label = "Record " + path[index + 1] + (" " + label.lower() if len(path) > index + 2 else "")
        elif "rules" in path:
            label = "Format rule"
        elif "charts" in path:
            label = "Chart"
        details.append(label + ": " + json.dumps(show(row["after"]), ensure_ascii=True))
    return [*heading, *details]


def apply(session, step):
    import routes_external as external
    held = step["preview"]
    # Every read used by the planner must still match, including permission/owner documents.
    for key, before_digest in held["reads"].items():
        try:
            current = session.runtime.get(key, fresh=True)
        except TypeError:
            current = session.runtime.get(key)
        if _digest(current) != before_digest:
            raise ValueError("The data changed after preview. Ask again to preview the change.")
    checked = prepare(session, step["tool"], step["resolvedArgs"], execution=held["execution"])
    if any(checked[key] != held[key] for key in ("documents", "notices", "audits")):
        raise ValueError("The data or permissions changed after preview. Ask again to preview the change.")
    # Commit the approved plan, rather than recomputing a plan inside a live writer.
    revisions = {key: session.runtime.revision(key) for key in held["documents"]}
    if any(not isinstance(rev, dict) or type(rev.get("rev")) is not int for rev in revisions.values()):
        raise ValueError("This store cannot safely confirm the preview. Nothing was changed.")
    for key, pair in held["documents"].items():
        def exact(current, pair=pair):
            return project(current, pair)
        session.runtime.update(key, exact, flush="sync", expect_rev=revisions[key]["rev"])
    if held["notices"]:
        import assistant_feed
        for notice in held["notices"]:
            assistant_feed.deliver_external(session.runtime, **notice)
    if held["audits"]:
        from core import audit
        for detail in held["audits"]:
            audit.record("external.query", session.tenant, session.uname, detail)
    return copy.deepcopy(held["result"])