We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
History entry fields consistency
We reached a situation where we have `record_id` different than `data.target.id`. This should not be possible oO

```json
        {
            "action": "update",
            "collection_id": "gfx",
            "date": "2016-11-21T10:09:00.225241",
            "id": "ae7e6831-11ad-49da-a739-a6fc25a913e8",
            "last_modified": 1479722940230,
            "record_id": "5bbd4751-5616-a3cc-e31c-1d04ade888bf",
            "resource_name": "record",
            "target": {
                "data": {
                    "blockID": "g1239",
                    "details": {
                        "bug": "https://bugzilla.mozilla.org/show_bug.cgi?id=838845",
                        "created": "2016-10-18T09:29:16Z",
                        "name": "GFX1-Val",
                        "who": "test",
                        "why": "test"
                    },
                    "devices": [
                        "0x4396"
                    ],
                    "driverVersion": "6.1.7601.18328",
                    "driverVersionComparator": "LESS_THAN_OR_EQUAL",
                    "enabled": true,
                    "feature": "DIRECT3D_10_LAYERS",
                    "featureStatus": "BLOCKED_DRIVER_VERSION",
                    "id": "cf217981-2c42-76bf-df19-4324a194a075",
                    "last_modified": 1479722940206,
                    "os": "All",
                    "schema": 1478713951432,
                    "vendor": "0x1002"
                },
                "permissions": {
                    "write": [
                        "basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f"
                    ]
                }
            },
            "timestamp": 1478713951077,
            "uri": "/buckets/staging/collections/gfx/records/cf217981-2c42-76bf-df19-4324a194a075",
            "user_id": "basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f"
        },

```
History entry fields consistency
We reached a situation where we have `record_id` different than `data.target.id`. This should not be possible oO

```json
        {
            "action": "update",
            "collection_id": "gfx",
            "date": "2016-11-21T10:09:00.225241",
            "id": "ae7e6831-11ad-49da-a739-a6fc25a913e8",
            "last_modified": 1479722940230,
            "record_id": "5bbd4751-5616-a3cc-e31c-1d04ade888bf",
            "resource_name": "record",
            "target": {
                "data": {
                    "blockID": "g1239",
                    "details": {
                        "bug": "https://bugzilla.mozilla.org/show_bug.cgi?id=838845",
                        "created": "2016-10-18T09:29:16Z",
                        "name": "GFX1-Val",
                        "who": "test",
                        "why": "test"
                    },
                    "devices": [
                        "0x4396"
                    ],
                    "driverVersion": "6.1.7601.18328",
                    "driverVersionComparator": "LESS_THAN_OR_EQUAL",
                    "enabled": true,
                    "feature": "DIRECT3D_10_LAYERS",
                    "featureStatus": "BLOCKED_DRIVER_VERSION",
                    "id": "cf217981-2c42-76bf-df19-4324a194a075",
                    "last_modified": 1479722940206,
                    "os": "All",
                    "schema": 1478713951432,
                    "vendor": "0x1002"
                },
                "permissions": {
                    "write": [
                        "basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f"
                    ]
                }
            },
            "timestamp": 1478713951077,
            "uri": "/buckets/staging/collections/gfx/records/cf217981-2c42-76bf-df19-4324a194a075",
            "user_id": "basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f"
        },

```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `kinto/plugins/history/listener.py`
Content:
```
1 from kinto.core.utils import instance_uri
2 from datetime import datetime
3 
4 
5 def on_resource_changed(event):
6     """
7     Everytime an object is created/changed/deleted, we create an entry in the
8     ``history`` resource. The entries are served as read-only in the
9     :mod:`kinto.plugins.history.views` module.
10     """
11     payload = event.payload
12     resource_name = payload['resource_name']
13     event_uri = payload['uri']
14 
15     bucket_id = None
16     bucket_uri = None
17     collection_uri = None
18 
19     storage = event.request.registry.storage
20     permission = event.request.registry.permission
21 
22     targets = []
23     for impacted in event.impacted_records:
24         target = impacted['new']
25         obj_id = target['id']
26 
27         try:
28             bucket_id = payload['bucket_id']
29         except KeyError:
30             # e.g. DELETE /buckets
31             bucket_id = obj_id
32         bucket_uri = instance_uri(event.request, 'bucket', id=bucket_id)
33 
34         if 'collection_id' in payload:
35             collection_id = payload['collection_id']
36             collection_uri = instance_uri(event.request,
37                                           'collection',
38                                           bucket_id=bucket_id,
39                                           id=collection_id)
40 
41         # On POST .../records, the URI does not contain the newly created
42         # record id.
43         parts = event_uri.split('/')
44         if resource_name in parts[-1]:
45             parts.append(obj_id)
46         else:
47             # Make sure the id is correct on grouped events.
48             parts[-1] = obj_id
49         uri = '/'.join(parts)
50         targets.append((uri, target))
51 
52     # Prepare a list of object ids to be fetched from permission backend,
53     # and fetch them all at once. Use a mapping for later convenience.
54     all_perms_objects_ids = [oid for (oid, _) in targets]
55     all_perms_objects_ids.append(bucket_uri)
56     if collection_uri is not None:
57         all_perms_objects_ids.append(collection_uri)
58     all_perms_objects_ids = list(set(all_perms_objects_ids))
59     all_permissions = permission.get_objects_permissions(all_perms_objects_ids)
60     perms_by_object_id = dict(zip(all_perms_objects_ids, all_permissions))
61 
62     bucket_perms = perms_by_object_id[bucket_uri]
63     collection_perms = {}
64     if collection_uri is not None:
65         collection_perms = perms_by_object_id[collection_uri]
66 
67     # The principals allowed to read the bucket and collection.
68     # (Note: ``write`` means ``read``)
69     read_principals = set(bucket_perms.get('read', []))
70     read_principals.update(bucket_perms.get('write', []))
71     read_principals.update(collection_perms.get('read', []))
72     read_principals.update(collection_perms.get('write', []))
73 
74     # Create a history entry for each impacted record.
75     for (uri, target) in targets:
76         obj_id = target['id']
77         # Prepare the history entry attributes.
78         perms = {k: list(v) for k, v in perms_by_object_id[uri].items()}
79         eventattrs = dict(**payload)
80         eventattrs.pop('bucket_id', None)
81         eventattrs.setdefault('%s_id' % resource_name, obj_id)
82         eventattrs['uri'] = uri
83         attrs = dict(date=datetime.now().isoformat(),
84                      target={'data': target, 'permissions': perms},
85                      **eventattrs)
86 
87         # Create a record for the 'history' resource, whose parent_id is
88         # the bucket URI (c.f. views.py).
89         # Note: this will be rolledback if the transaction is rolledback.
90         entry = storage.create(parent_id=bucket_uri,
91                                collection_id='history',
92                                record=attrs)
93 
94         # The read permission on the newly created history entry is the union
95         # of the record permissions with the one from bucket and collection.
96         entry_principals = set(read_principals)
97         entry_principals.update(perms.get('read', []))
98         entry_principals.update(perms.get('write', []))
99         entry_perms = {'read': list(entry_principals)}
100         # /buckets/{id}/history is the URI for the list of history entries.
101         entry_perm_id = '/buckets/%s/history/%s' % (bucket_id, entry['id'])
102         permission.replace_object_permissions(entry_perm_id, entry_perms)
103
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/kinto/plugins/history/listener.py b/kinto/plugins/history/listener.py
--- a/kinto/plugins/history/listener.py
+++ b/kinto/plugins/history/listener.py
@@ -77,8 +77,9 @@
         # Prepare the history entry attributes.
         perms = {k: list(v) for k, v in perms_by_object_id[uri].items()}
         eventattrs = dict(**payload)
+        eventattrs.pop('timestamp', None)  # Already in target `last_modified`.
         eventattrs.pop('bucket_id', None)
-        eventattrs.setdefault('%s_id' % resource_name, obj_id)
+        eventattrs['%s_id' % resource_name] = obj_id
         eventattrs['uri'] = uri
         attrs = dict(date=datetime.now().isoformat(),
                      target={'data': target, 'permissions': perms},

{"golden_diff": "diff --git a/kinto/plugins/history/listener.py b/kinto/plugins/history/listener.py\n--- a/kinto/plugins/history/listener.py\n+++ b/kinto/plugins/history/listener.py\n@@ -77,8 +77,9 @@\n         # Prepare the history entry attributes.\n         perms = {k: list(v) for k, v in perms_by_object_id[uri].items()}\n         eventattrs = dict(**payload)\n+        eventattrs.pop('timestamp', None)  # Already in target `last_modified`.\n         eventattrs.pop('bucket_id', None)\n-        eventattrs.setdefault('%s_id' % resource_name, obj_id)\n+        eventattrs['%s_id' % resource_name] = obj_id\n         eventattrs['uri'] = uri\n         attrs = dict(date=datetime.now().isoformat(),\n                      target={'data': target, 'permissions': perms},\n", "issue": "History entry fields consistency\nWe reached a situation where we have `record_id` different than `data.target.id`. This should not be possible oO\r\n\r\n```json\r\n        {\r\n            \"action\": \"update\",\r\n            \"collection_id\": \"gfx\",\r\n            \"date\": \"2016-11-21T10:09:00.225241\",\r\n            \"id\": \"ae7e6831-11ad-49da-a739-a6fc25a913e8\",\r\n            \"last_modified\": 1479722940230,\r\n            \"record_id\": \"5bbd4751-5616-a3cc-e31c-1d04ade888bf\",\r\n            \"resource_name\": \"record\",\r\n            \"target\": {\r\n                \"data\": {\r\n                    \"blockID\": \"g1239\",\r\n                    \"details\": {\r\n                        \"bug\": \"https://bugzilla.mozilla.org/show_bug.cgi?id=838845\",\r\n                        \"created\": \"2016-10-18T09:29:16Z\",\r\n                        \"name\": \"GFX1-Val\",\r\n                        \"who\": \"test\",\r\n                        \"why\": \"test\"\r\n                    },\r\n                    \"devices\": [\r\n                        \"0x4396\"\r\n                    ],\r\n                    \"driverVersion\": \"6.1.7601.18328\",\r\n                    \"driverVersionComparator\": \"LESS_THAN_OR_EQUAL\",\r\n                    \"enabled\": true,\r\n                    \"feature\": \"DIRECT3D_10_LAYERS\",\r\n                    \"featureStatus\": \"BLOCKED_DRIVER_VERSION\",\r\n                    \"id\": \"cf217981-2c42-76bf-df19-4324a194a075\",\r\n                    \"last_modified\": 1479722940206,\r\n                    \"os\": \"All\",\r\n                    \"schema\": 1478713951432,\r\n                    \"vendor\": \"0x1002\"\r\n                },\r\n                \"permissions\": {\r\n                    \"write\": [\r\n                        \"basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f\"\r\n                    ]\r\n                }\r\n            },\r\n            \"timestamp\": 1478713951077,\r\n            \"uri\": \"/buckets/staging/collections/gfx/records/cf217981-2c42-76bf-df19-4324a194a075\",\r\n            \"user_id\": \"basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f\"\r\n        },\r\n\r\n```\nHistory entry fields consistency\nWe reached a situation where we have `record_id` different than `data.target.id`. This should not be possible oO\r\n\r\n```json\r\n        {\r\n            \"action\": \"update\",\r\n            \"collection_id\": \"gfx\",\r\n            \"date\": \"2016-11-21T10:09:00.225241\",\r\n            \"id\": \"ae7e6831-11ad-49da-a739-a6fc25a913e8\",\r\n            \"last_modified\": 1479722940230,\r\n            \"record_id\": \"5bbd4751-5616-a3cc-e31c-1d04ade888bf\",\r\n            \"resource_name\": \"record\",\r\n            \"target\": {\r\n                \"data\": {\r\n                    \"blockID\": \"g1239\",\r\n                    \"details\": {\r\n                        \"bug\": \"https://bugzilla.mozilla.org/show_bug.cgi?id=838845\",\r\n                        \"created\": \"2016-10-18T09:29:16Z\",\r\n                        \"name\": \"GFX1-Val\",\r\n                        \"who\": \"test\",\r\n                        \"why\": \"test\"\r\n                    },\r\n                    \"devices\": [\r\n                        \"0x4396\"\r\n                    ],\r\n                    \"driverVersion\": \"6.1.7601.18328\",\r\n                    \"driverVersionComparator\": \"LESS_THAN_OR_EQUAL\",\r\n                    \"enabled\": true,\r\n                    \"feature\": \"DIRECT3D_10_LAYERS\",\r\n                    \"featureStatus\": \"BLOCKED_DRIVER_VERSION\",\r\n                    \"id\": \"cf217981-2c42-76bf-df19-4324a194a075\",\r\n                    \"last_modified\": 1479722940206,\r\n                    \"os\": \"All\",\r\n                    \"schema\": 1478713951432,\r\n                    \"vendor\": \"0x1002\"\r\n                },\r\n                \"permissions\": {\r\n                    \"write\": [\r\n                        \"basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f\"\r\n                    ]\r\n                }\r\n            },\r\n            \"timestamp\": 1478713951077,\r\n            \"uri\": \"/buckets/staging/collections/gfx/records/cf217981-2c42-76bf-df19-4324a194a075\",\r\n            \"user_id\": \"basicauth:3fa5a813e05061f219048d6b0d0a398ed99868213af0571886b76c2423dc097f\"\r\n        },\r\n\r\n```\n", "before_files": [{"content": "from kinto.core.utils import instance_uri\nfrom datetime import datetime\n\n\ndef on_resource_changed(event):\n    \"\"\"\n    Everytime an object is created/changed/deleted, we create an entry in the\n    ``history`` resource. The entries are served as read-only in the\n    :mod:`kinto.plugins.history.views` module.\n    \"\"\"\n    payload = event.payload\n    resource_name = payload['resource_name']\n    event_uri = payload['uri']\n\n    bucket_id = None\n    bucket_uri = None\n    collection_uri = None\n\n    storage = event.request.registry.storage\n    permission = event.request.registry.permission\n\n    targets = []\n    for impacted in event.impacted_records:\n        target = impacted['new']\n        obj_id = target['id']\n\n        try:\n            bucket_id = payload['bucket_id']\n        except KeyError:\n            # e.g. DELETE /buckets\n            bucket_id = obj_id\n        bucket_uri = instance_uri(event.request, 'bucket', id=bucket_id)\n\n        if 'collection_id' in payload:\n            collection_id = payload['collection_id']\n            collection_uri = instance_uri(event.request,\n                                          'collection',\n                                          bucket_id=bucket_id,\n                                          id=collection_id)\n\n        # On POST .../records, the URI does not contain the newly created\n        # record id.\n        parts = event_uri.split('/')\n        if resource_name in parts[-1]:\n            parts.append(obj_id)\n        else:\n            # Make sure the id is correct on grouped events.\n            parts[-1] = obj_id\n        uri = '/'.join(parts)\n        targets.append((uri, target))\n\n    # Prepare a list of object ids to be fetched from permission backend,\n    # and fetch them all at once. Use a mapping for later convenience.\n    all_perms_objects_ids = [oid for (oid, _) in targets]\n    all_perms_objects_ids.append(bucket_uri)\n    if collection_uri is not None:\n        all_perms_objects_ids.append(collection_uri)\n    all_perms_objects_ids = list(set(all_perms_objects_ids))\n    all_permissions = permission.get_objects_permissions(all_perms_objects_ids)\n    perms_by_object_id = dict(zip(all_perms_objects_ids, all_permissions))\n\n    bucket_perms = perms_by_object_id[bucket_uri]\n    collection_perms = {}\n    if collection_uri is not None:\n        collection_perms = perms_by_object_id[collection_uri]\n\n    # The principals allowed to read the bucket and collection.\n    # (Note: ``write`` means ``read``)\n    read_principals = set(bucket_perms.get('read', []))\n    read_principals.update(bucket_perms.get('write', []))\n    read_principals.update(collection_perms.get('read', []))\n    read_principals.update(collection_perms.get('write', []))\n\n    # Create a history entry for each impacted record.\n    for (uri, target) in targets:\n        obj_id = target['id']\n        # Prepare the history entry attributes.\n        perms = {k: list(v) for k, v in perms_by_object_id[uri].items()}\n        eventattrs = dict(**payload)\n        eventattrs.pop('bucket_id', None)\n        eventattrs.setdefault('%s_id' % resource_name, obj_id)\n        eventattrs['uri'] = uri\n        attrs = dict(date=datetime.now().isoformat(),\n                     target={'data': target, 'permissions': perms},\n                     **eventattrs)\n\n        # Create a record for the 'history' resource, whose parent_id is\n        # the bucket URI (c.f. views.py).\n        # Note: this will be rolledback if the transaction is rolledback.\n        entry = storage.create(parent_id=bucket_uri,\n                               collection_id='history',\n                               record=attrs)\n\n        # The read permission on the newly created history entry is the union\n        # of the record permissions with the one from bucket and collection.\n        entry_principals = set(read_principals)\n        entry_principals.update(perms.get('read', []))\n        entry_principals.update(perms.get('write', []))\n        entry_perms = {'read': list(entry_principals)}\n        # /buckets/{id}/history is the URI for the list of history entries.\n        entry_perm_id = '/buckets/%s/history/%s' % (bucket_id, entry['id'])\n        permission.replace_object_permissions(entry_perm_id, entry_perms)\n", "path": "kinto/plugins/history/listener.py"}], "after_files": [{"content": "from kinto.core.utils import instance_uri\nfrom datetime import datetime\n\n\ndef on_resource_changed(event):\n    \"\"\"\n    Everytime an object is created/changed/deleted, we create an entry in the\n    ``history`` resource. The entries are served as read-only in the\n    :mod:`kinto.plugins.history.views` module.\n    \"\"\"\n    payload = event.payload\n    resource_name = payload['resource_name']\n    event_uri = payload['uri']\n\n    bucket_id = None\n    bucket_uri = None\n    collection_uri = None\n\n    storage = event.request.registry.storage\n    permission = event.request.registry.permission\n\n    targets = []\n    for impacted in event.impacted_records:\n        target = impacted['new']\n        obj_id = target['id']\n\n        try:\n            bucket_id = payload['bucket_id']\n        except KeyError:\n            # e.g. DELETE /buckets\n            bucket_id = obj_id\n        bucket_uri = instance_uri(event.request, 'bucket', id=bucket_id)\n\n        if 'collection_id' in payload:\n            collection_id = payload['collection_id']\n            collection_uri = instance_uri(event.request,\n                                          'collection',\n                                          bucket_id=bucket_id,\n                                          id=collection_id)\n\n        # On POST .../records, the URI does not contain the newly created\n        # record id.\n        parts = event_uri.split('/')\n        if resource_name in parts[-1]:\n            parts.append(obj_id)\n        else:\n            # Make sure the id is correct on grouped events.\n            parts[-1] = obj_id\n        uri = '/'.join(parts)\n        targets.append((uri, target))\n\n    # Prepare a list of object ids to be fetched from permission backend,\n    # and fetch them all at once. Use a mapping for later convenience.\n    all_perms_objects_ids = [oid for (oid, _) in targets]\n    all_perms_objects_ids.append(bucket_uri)\n    if collection_uri is not None:\n        all_perms_objects_ids.append(collection_uri)\n    all_perms_objects_ids = list(set(all_perms_objects_ids))\n    all_permissions = permission.get_objects_permissions(all_perms_objects_ids)\n    perms_by_object_id = dict(zip(all_perms_objects_ids, all_permissions))\n\n    bucket_perms = perms_by_object_id[bucket_uri]\n    collection_perms = {}\n    if collection_uri is not None:\n        collection_perms = perms_by_object_id[collection_uri]\n\n    # The principals allowed to read the bucket and collection.\n    # (Note: ``write`` means ``read``)\n    read_principals = set(bucket_perms.get('read', []))\n    read_principals.update(bucket_perms.get('write', []))\n    read_principals.update(collection_perms.get('read', []))\n    read_principals.update(collection_perms.get('write', []))\n\n    # Create a history entry for each impacted record.\n    for (uri, target) in targets:\n        obj_id = target['id']\n        # Prepare the history entry attributes.\n        perms = {k: list(v) for k, v in perms_by_object_id[uri].items()}\n        eventattrs = dict(**payload)\n        eventattrs.pop('timestamp', None)  # Already in target `last_modified`.\n        eventattrs.pop('bucket_id', None)\n        eventattrs['%s_id' % resource_name] = obj_id\n        eventattrs['uri'] = uri\n        attrs = dict(date=datetime.now().isoformat(),\n                     target={'data': target, 'permissions': perms},\n                     **eventattrs)\n\n        # Create a record for the 'history' resource, whose parent_id is\n        # the bucket URI (c.f. views.py).\n        # Note: this will be rolledback if the transaction is rolledback.\n        entry = storage.create(parent_id=bucket_uri,\n                               collection_id='history',\n                               record=attrs)\n\n        # The read permission on the newly created history entry is the union\n        # of the record permissions with the one from bucket and collection.\n        entry_principals = set(read_principals)\n        entry_principals.update(perms.get('read', []))\n        entry_principals.update(perms.get('write', []))\n        entry_perms = {'read': list(entry_principals)}\n        # /buckets/{id}/history is the URI for the list of history entries.\n        entry_perm_id = '/buckets/%s/history/%s' % (bucket_id, entry['id'])\n        permission.replace_object_permissions(entry_perm_id, entry_perms)\n", "path": "kinto/plugins/history/listener.py"}]}