Index: /Dev/trunk/Gruntfile.js
===================================================================
--- /Dev/trunk/Gruntfile.js	(revision 521)
+++ /Dev/trunk/Gruntfile.js	(revision 522)
@@ -31,5 +31,5 @@
                        ['path-check:foreman'
                        ,'compile'
-                       ,'foreman:dev']);
+                       ,'foreman:run-src']);
     grunt.registerTask('build',
                        "Compile and make a Dojo build of the client (compress sources).",
@@ -43,5 +43,5 @@
                        ['path-check:foreman'
                        ,'build'
-                       ,'foreman:build']);
+                       ,'foreman:run-build']);
     grunt.registerTask('deploy',
                        "Make a build and deploy it to Heroku.",
@@ -50,5 +50,12 @@
                        ,'svninfo'
                        ,'build'
-                       ,'git_deploy:deploy']);
+                       ,'git_deploy:deploy-build']);
+    grunt.registerTask('deploy-src',
+                       "Deploy sources to Heroku (FOR DEBUGGING ONLY).",
+                       ['path-check:svn'
+                       ,'path-check:git'
+                       ,'svninfo'
+                       ,'compile'
+                       ,'git_deploy:deploy-src']);
     // database management
     grunt.registerTask('db-backup-cloud-to-dated-local',
@@ -126,10 +133,10 @@
         },
         foreman: {
-            dev: {
+            'run-src': {
                 options: {
                     cwd: srcDir
                 }
             },
-            build: {
+            'run-build': {
                 options: {
                     cwd: buildDir
@@ -138,11 +145,20 @@
         },
         git_deploy: {
-            deploy: {
+            'deploy-build': {
                 options: {
                     url: 'git@heroku.com:quod-erat.git',
                     branch: 'master',
-                    message: "Deployment of revision <%= svninfo.rev %> on <%= grunt.template.today() %>."
+                    message: "Build deployment of revision <%= svninfo.rev %> on <%= grunt.template.today() %>."
                 },
                 src: buildDir
+            },
+            
+            'deploy-src': {
+                options: {
+                    url: 'git@heroku.com:quod-erat.git',
+                    branch: 'master',
+                    message: "Source deployment of revision <%= svninfo.rev %> on <%= grunt.template.today() %>."
+                },
+                src: srcDir
             }
         },
Index: /Dev/trunk/src/client/qed-client/xhr.js
===================================================================
--- /Dev/trunk/src/client/qed-client/xhr.js	(revision 521)
+++ /Dev/trunk/src/client/qed-client/xhr.js	(revision 522)
@@ -2,26 +2,34 @@
     "./session",
     "dojo/Deferred",
+    "dojo/_base/array",
     "dojo/_base/lang",
-    "dojo/on",
-    "dojo/_base/xhr"
-], function(session, Deferred, lang, on, xhr) {
+    "dojo/_base/xhr",
+    "dojo/on"
+], function(session, Deferred, array, lang, xhr, on) {
 
-    var user = session.get();
+
     var queue = [];
-    
-    on(session, 'change', function(newUser){
+    var user;
+    var generation = 0;
+
+    on(session, 'change', setUser);
+    setUser(session.get());
+
+    function setUser(newUser) {
         user = newUser;
-        retry();
-    });
-
-    function retry() {
-        if (user && queue.length > 0) {
-            var item = queue.shift();
-            console.log("Retry",item.options.url);
-            real_request(item);
+        generation += 1;
+        if ( user ) {
+            // we make a local copy, just in case the requests cause
+            // events to happen
+            var q = queue;
+            queue = [];
+            array.forEach(q,real_request);
+        } else {
+            session.restore();
         }
     }
-    
+
     function real_request(item) {
+        var req_generation = generation;
         var req = xhr(item.method,lang.mixin(item.options||{},{
             failOk: true
@@ -29,19 +37,31 @@
         item.promise.ioArgs = req.ioArgs;
 
-        req.then(function(result){
+        console.log("Do request",item.options.url);
+        req.then(function(result) {
             item.dfd.resolve(result);
-            retry();
         }, function(error){
             if ( error.response.status === 401 ) {
-                queue.unshift(item);
-                session.restore();
+                if ( user ) {
+                    if ( req_generation === generation ) {
+                        console.log("Retry after reauthentication",item.options.url);
+                        // we need to reauth
+                        queue.push(item);
+                        setUser(null);
+                    } else {
+                        console.log("Retry immediately",item.options.url);
+                        // we already did, redo request right away
+                        real_request(item);
+                    }
+                } else {
+                    console.log("Retry when authenticated",item.options.url);
+                    queue.push(item);
+                }
             } else {
                 item.dfd.reject(error);
-                retry();
             }
         });
     }
     
-    var _request = function(method, options) {
+    var _xhr = function(method, options) {
         var item = {
             method: method,
@@ -50,11 +70,10 @@
         };
         item.promise = lang.delegate(item.dfd.promise);
-        // only do the request directly if we are authenticated and
-        // there are no earlier requests queued.
-        if ( user && queue.length === 0 ) {
-            console.log("Request",options.url);
+        // only do the request directly if we are authenticated
+        if ( user ) {
+            console.log("Immediate",options.url);
             real_request(item);
         } else {
-            console.log("Push",options.url);
+            console.log("Delay until authenticated",options.url);
             queue.push(item);
         }
@@ -62,4 +81,4 @@
     };
 
-    return _request;
+    return _xhr;
 });
Index: /Dev/trunk/src/server/util/couch.js
===================================================================
--- /Dev/trunk/src/server/util/couch.js	(revision 521)
+++ /Dev/trunk/src/server/util/couch.js	(revision 522)
@@ -77,5 +77,5 @@
         };
     }, function(err) {
-        return Q.reject(err);
+        return Q.reject({error:err.message});
     });
     return res;
